Constructors

What is Constructor?
Constructors are special methods used to initialize types and create instances of types. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read. If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values Static classes and structs can also have constructors.
What is Private Constructor?
A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. Private constructors are used to prevent creating instances of a class when there are no instance fields or methods or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.
What is static constuctor?
A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced. Static constructors have the following properties: · A static constructor does not take access modifiers or have parameters. · A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. · A static constructor cannot be called directly. · The user has no control on when the static constructor is executed in the program. · A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file. · Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method. Explain Guidelines for constructors? · Consider providing simple, ideally default, constructors. A simple constructor has a very small number of parameters, and all parameters are primitive types or enumerations. · Consider using a static factory method instead of a constructor if the semantics of the desired operation do not map directly to the construction of a new instance, or if following the constructor design guidelines feels unnatural. · Do use constructor parameters as shortcuts for setting main properties. · Do use the same name for constructor parameters and a property, if the constructor parameters are used to simply set the property. The only difference between such parameters and the properties should be casing. · Do minimal work in the constructor. Constructors should not do much work other than to capture the constructor parameters. The cost of any other processing should be delayed until required. · Do throw exceptions from instance constructors if appropriate. · Do explicitly declare the public default constructor in classes, if such a constructor is required. · Avoid having default constructors on structures. · Do not call virtual members on an object inside its constructors.
How do I call one constructor from another in C#?
You use : base (parameters) or : this (parameters) just before the actual code for the constructor, depending on whether you want to call a constructor in the base class or in this class.