We Have atable named "User_General_Info" . Then If we need to Add a column to the table with a default date(e,g, System Date) then we can write in this way....
ALTER TABLE [User_General_Info]
ADD Reg_Date datetime
CONSTRAINT DF_User_General_Info_Reg_Date DEFAULT (getdate()) NOT NULL
Static keyword is widely used to share the same field or method among all the objects of the class. The actual goal of the static keyword is to share a single data over all the objects.
There are three types of sharing using the static keyword. They are –
ØStatic Method
ØStatic Field
ØStatic Class
Now I am going to give a short brief on these three types-
Static Method
A Static Method can be accessed from outside the class without creating any object of this class. This Static Method can be accessed by directly by the name of the static method followed by the . (dot operator) and the class name.
For example, we can consider the Sqrt method of the Math class. This is how the Sqrt method of the real Math class is defined :
class Math
{
……….
public static double Sqrt (double d)
{
………………………
}
}
From the previous example you can notify that the Sqrt method is declared as static so that it can be accessed by using the class name directly,no object of the Math class is required to access the static method.
So, here I will like to some general properties of a Static Method :
It can access only the static fields of the class.
It can directly invoke the methods that are defined as static.
Static Fields
A static field is shared among all the objects of the of the class. So if an object change this the value then all the object of this class will get the changed value.
Look at the example below –
classPoint
{
public Point()
{
this.x = -1;
this.y = -1;
Console.WriteLine("Deafult Constructor Called");
objectCount++;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
Console.WriteLine("x = {0} , y = {1}", x, y);
objectCount++;
}
privateint x, y;
public staticint objectCount = 0;
}
Now if we create three objects for this class –
Point origin1 = newPoint(); // objectCount = 1
Point origin2 = newPoint(); // objectCount = 2
Point origin3 = newPoint(); // objectCount = 3
Here these three objects share the same field so evry time the static field objectCount is incremented.
Static Class
A static class is used to hold all the utility methods and fields. A static Class has some properties –
All the methods and fields inside the class must be declared as static.
A static class cannot contain any instance method or data.
No object can be created (even using the “new” keyword) of this class.
It can has a default constructor and it is also static.
Constructor is a simple method inside a class. Except it has some exceptions:
ØConstructor has the same name as the class.
ØConstructor does not return value even void.
ØConstructor can be built with parameter or without parameter.
ØEvery class has a default constructor Even if did not create it. If the developer does not create a constructor then the .NET compiler create a default constructor, this deafault constructor actually does nothing.
You can easily create a default constructor by creating a method like this :
classPoint
{
public Point()
{
this.x = -1;
this.y = -1;
}
privateint x, y;
}
Have a look on the example, there are two private variable x and y. So when an instance of the class point will be created the constructor will be fired and the variable x and y will get their value as -1 , -1 accordingly.
[ Here one thing is noticable, why I have used “this” keyword? I have told earlier that a constructor may be paremeterized or non-parameterized . if it is parameterized and I write it as
classPoint
{
public Point(int x , int y)// do not write as this
{
x = x;//what is the parameter and what is the feild
y = y;// what is the parameter and what is the feild
}
privateint x, y;
}
Though the program will run but it is little ambiguous which one the parameter andwhich one is the field.
The “this” keyword indicates that – “this is a field”]
Now you can initialize your object by the constructor Point(). Like this way –
namespace ConAppCH7CCons
{
classConstructorExample
{
staticvoid Main(string[] args)
{
Point origin = newPoint();
Console.ReadLine();
}
}
}
So, when the “origin” object is created it it is initialized with the value
x=-1
y=-1
Overriding a Constructor
Overriding is a feture of OOP and an example of Polymorphism. Constructor overloading is very simple as method overloading. You can have different version of constructors and they can vary by the number of parameter and type of parameter.
From the preeciding example , let uss, you do not want to initialize your x and y value with different values and in the mean time you want to keep an option that the user can run the program without any value. In this situation you can code like this :
namespace ConAppCH7CCons
{
classPoint
{
public Point()
{
this.x = -1;
this.y = -1;
Console.WriteLine("Deafult Constructor Called");
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
Console.WriteLine("x = {0} , y = {1}", x, y);
}
privateint x, y;
}
}
Now you have two different versions of constructor. You can create objects like bellow :
namespace ConAppCH7CCons
{
classConstructorExample
{
staticvoid Main(string[] args)
{
Point origin = newPoint();
Point bottomRight = newPoint(1024, 1280);
Console.ReadLine();
}
}
}
So when when the object “origin” is initiated then
x=-1
y=-1
and when the object “bottomRight” is initiated then
x=1024
y=1280
Example
Here I will show an example related on constructor. This example is for calculate the distance between two given points-