Here are some gotchas which we need to watch out while using public and private constructor.
Found a really good and simple article on Code Project:
http://www.codeproject.com/Articles/7011/An-Intro-to-Constructors-in-C
Short notes from the article
Public Constructor
Note: If we do not provide initializer referring to the base class constructor then it executes the no parameter constructor of the base class.
Note one thing here: we are not making any explicit call to the constructor of base class neither by initializer nor by thebase
keyword, but it is still executing. This is the normal behavior of the constructor.
Private Constructor
Private constructors, the constructors with the "private
" access modifier, are a bit special case. It is because we can neither create the object of the class, nor can we inherit the class with only private
constructors. But yes, we can have the set of public
constructors along with the private
constructors in the class and the public
constructors can access the private
constructors from within the class through constructor chaining.
It is possible to have the class with only the private constructors. But yes as I said, such class can neither be instantiated nor be inherited. If we try to inherit the class with only private constructors then we will get the same error as above. Also recall, once you provide constructor from your side the compiler will not add the no-parameter public constructor to your class.
For example if you have a class that should only be created through factory methods. Or if you have overloads of the constructor, and some of them should only be used by the other constructors. Probably other reasons as well =)
Static Constructor
Notes for Static Constructors:
- There can be only one static constructor in the class.
- The static constructor should be without parameters.
- It can only access the static members of the class.
- There should be no access modifier in static constructor definition.
What is the use of static constructor ?
An amazing answer could be found at:
A static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc.
It is run automatically by the runtime the first time it is needed (the exact rules there are complicated (see "beforefieldinit"), and changed subtly between CLR2 and CLR4). Unless you abuse reflection, it is guaranteed to run at most once (even if two threads arrive at the same time).
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.