Static Constructor in C# and their Usages
Let's start with constructor definition.Constructor is a special method of a class that is invoked automatically whenever a new instance/object of class is created.Constructors are responsible for object initialization and memory allocation for all class members. A class without constructor does't mean that it has no constructor .Compiler will automatically create a default constructor for that class.
Static constructor
Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.
Here are some important point about static constructor -
- Static constructor of a class executes before any instance of the class is created.
- Static constructor of a class executes before any of the static members for the class are referenced.
- Static constructor of a class executes after the static field initializes.
- Static constructor of a class executes only once.
- Static constructor does not have any access modifiers or have parameters.
- Static constructor cannot be called explicitly.
- User has no control on when the static constructor is executed in the program.
Hope you have like this tutorial.
Thanks.
Thanks.
Static Constructor in C# and their Usages
Reviewed by CodiBucket
on
02:32
Rating:
No comments: