Static Constructors
Constructors can also be declared static. While an instance constructor initializes each new instance of a class, a static constructor
initializes items at the class level. Generally, static constructors initialize the static fields of the class.
It's a little bit confusing, but lets take a look to example code
The code will display:
-
Static constructors are like instance constructors in the following ways:
- The name of the static constructor must be the same as the name of the class.
- The constructor cannot return a value.
-
Static constructors are unlike instance constructors in the following ways:
- Static constructors use the static keyword in the declaration.
- There can only be a single static constructor for a class, and it cannot have parameters.
- Static constructors cannot have accessibility modifiers.
- A class can have both a static constructor and instance constructors.
-
Like static methods, a static constructor cannot access instance members of its class and cannot use
the
this
accessor. -
You cannot explicitly call static constructors from your program. They’re called automatically by the system, at some time
- Before any instance of the class is created
- Before any static member of the class is referenced
class [Class Name]
{
static [Class Name]()
{
... // Do all the static initializations.
}
...
}
It's a little bit confusing, but lets take a look to example code
class RandomNumberClass { private static Random RandomKey; //Private static field static RandomNumberClass() //Static constructor { Console.WriteLine("static RandomNumberClass Constructor"); RandomKey = new Random(); //Initialize RandomKey } public RandomNumberClass() //Non-Static constructor { Console.WriteLine("non-static RandomNumberClass Constructor"); } public int GetRandomNumber() { return RandomKey.Next(); } } class Program { static void Main() { RandomNumberClass a = new RandomNumberClass(); RandomNumberClass b = new RandomNumberClass(); Console.WriteLine("Next Random #: {0}", a.GetRandomNumber()); Console.WriteLine("Next Random #: {0}", b.GetRandomNumber()); } }
The code will display:
static RandomNumberClass Constructor
non-static RandomNumberClass Constructor
non-static RandomNumberClass Constructor
Next Random #: 2033553409
Next Random #: 160144755
non-static RandomNumberClass Constructor
non-static RandomNumberClass Constructor
Next Random #: 2033553409
Next Random #: 160144755
Line |
RandomNumberClass a = new RandomNumberClass(); this code invoke the non-static constructor. But system will
invoke the static constructor just before the command is executed.
So in the display will output: "static RandomNumberClass Constructor", then "non-static RandomNumberClass Constructor".
|
Line | This code invoke the non-static constructor. But different from prior command, system will not call static constructor no more. So, the display will only shows: "non-static RandomNumberClass Constructor"; |
Line |
Calls GetRandomNumber() , will return random number. Your output may be different from mine.
|
Object Initializers
Object initializer extends the constructor syntax by placing a list of member initializations at the end of the expression.
An object initializer allows you to set the values of fields and properties when creating a new instance of an object.
Use following syntax to initialize object:
Important things to know about object initializers are the following:
The code will display:
Use following syntax to initialize object:
new [Type Name] ([Parameter List]){[Field or Property] = [Value],...,[Field or Property] = [Value]}
Important things to know about object initializers are the following:
- The fields and properties being initialized must be accessible to the code creatingthe object.
- The initialization occurs after the constructor has finished execution, so the values might have been set in the constructor and then reset to the same or a different value in the object initialize.
class circle { private const float PI = 3.416F; public int radius; //should be accessible for object initializer public string color; //should be accessible for object initializer public float getArea() { return PI * radius * radius; } public void showCircle() { Console.WriteLine("radius: " + radius + " color: " + color + " area:" + getArea()); } } class Program { static void Main() { circle circle1 = new circle() { radius = 20, color = "yellow" } ; circle circle2 = new circle() { radius = 5, color = "blue" }; Console.WriteLine("showing circle1"); circle1.showCircle(); Console.WriteLine("showing circle2"); circle2.showCircle(); Console.Read(); } }
The code will display:
showing circle1
radius: 20 color: yellow area:1366,4
showing circle2
radius: 5 color: blue area:85,39999
radius: 20 color: yellow area:1366,4
showing circle2
radius: 5 color: blue area:85,39999
0 comments:
Post a Comment