Sunday, January 23, 2011

C# - Static Constructors

• You declare a static constructor by "static" keyword in front of the constructor name.
• To initialize static fields in a class you can use static constructor.
• A static constructor is called before an instance of a class is created
• A static constructor is called before a static member is called.
• A static constructor is called before the static constructor of a derived class.
• A static constructor does not take access modifiers or have parameters.
• A static constructor cannot be called directly.
• They are called only once.
• Can't access anything but static members.

class MyClass
{
// Static constructor
static MyClass()
{
//...
}
}

Share/Bookmark

No comments: