Is there a work around on how to create a constructor for static class?
I need some data to be loaded when the class is initialized but I need one and only one object.
Is there a work around on how to create a constructor for static class?
I need some data to be loaded when the class is initialized but I need one and only one object.
C# has a static constructor for this purpose.
From MSDN:
MSDN link
.
Static Constructor
A constructor declared using static modifier is a static constructor. A static constructor is use to initialize static data or to perform a particular action that need to be performed only once in life cycle of class. Static constructor is first block of code to execute in class. Static constructor executes one and only one time in life cycle of class. It is called automatically. Static constructor does not take any parameters. It has no access specifiers. It is not called directly.
C# has a static constructor for this purpose.
You can use static constructor to initialization static variable. Static constructor will be entry point for your class
Yes, a static class can have static constructor, and the use of this constructor is initialization of static member.
and static constructor get called only once when you have access any type member of static class with class name Class1
Suppose you are accessing the first EmployeeName field then constructor get called this time, after that it will not get called, even if you will access same type member.
A static constructor looks like this
It is executed only once when the type is first used. All classes can have static constructors, not just static classes.