If you go through any standard C# books you will see the constructors and so I would not get into the details of the constructors but what is more important to me is the use of static constructors which is somethine new in C# or you can say that its a copy of initiliazation block in Java.
Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization. If a class is not defined with the constructor then the CLR (Common Language Runtime) will provide an implicit constructor which is called as Default Constructor. A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.
Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization. If a class is not defined with the constructor then the CLR (Common Language Runtime) will provide an implicit constructor which is called as Default Constructor. A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.
- Constructors do not return a value.
- Constructors can be overloaded.
- If a class is defined with static and Non-static constructors then the privilege will be given to the Non-static constructors.
- Public : A constructor that is defined as public will be called whenever a class is instantiated.
- Protected : A constructor is defined as protected in such cases where the base class will initialize on its own whenever derived types of it are created.
- Private : A constructor is defined as private in such cases whenever a class which contains only static members has to be accessed will avoid the creation of the object for the class.
- Internal : An internal constructor can be used to limit concrete implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly.
- External : When a constructor is declared using an extern modifier, the constructor is said to be an external constructor.
Types of Constructors
- Static : Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.
Code: CSharpStatic ClassName()
{
//Initialization statements;
} - Non-Static : are used for initializing the Non-Static and Static members of a class. These will be invoked everytime a new object is defined for a class.
Code: CSharpPublic ClassName([argsInfo])
{
//Initialization statements;
}

