Introduction
- A constructor has the same name of the class or struct.
- Contructors do not have a return type.
- C# will create one by default that instantiates the object and sets the member variables to the default values as listed.
- Constructors can be overloaded by the number and type of parameter.
Instance Constructor
Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class.
- class CoOrds
- {
- public int x, y;
- // default constructor
- public CoOrds()
- {
- x = 0;
- y = 0;
- }
- }
This instance constructor is called whenever an object based on the CoOrds class is created. A constructor like this one that takes no arguments, is called a default constructor.
Parameterized Constructor
A constructor with at least one parameter is called a parametrized constructor. The advantage of a parametrized constructor is that you can initialize each instance of the class to different values.
- // A constructor with two arguments: public CoOrds(int x, int y) { this.x = x; this.y = y; }
This allows CoOrd objects to be created with default or specific initial values, like this:
- CoOrds p2 = new CoOrds(5, 3);
Static Constructor
To initialize a static class, or static variables in a non-static class, you must define a static constructor. A static constructor is used to initialize any static data, or to perform a specific action that needs to be done 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.
- If a static constructor throws an exception, the runtime will not invoke it a second time and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
- class SimpleClass
- {
- // Static variable that must be initialized at run time.
- static readonly long baseline;
- // Static constructor is called at most one time, before any
- // instance constructor is invoked or member is accessed.
- static SimpleClass()
- {
- baseline = DateTime.Now.Ticks;
- }
- }
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.
- class NLog
- {
- // Private Constructor:
- private NLog() {}
- public static double e = Math.E; //2.71828...
- }

Jaipal ReddyPosted Jul 1, 2015, 12:15 AM
Good start.
Chiheb ChebbiPosted Jun 29, 2015, 10:16 AM
nice introduction
RakeshPosted Jun 29, 2015, 4:58 AM
Good One welcome to C# Corner
Gopi ChandPosted Jun 29, 2015, 4:31 AM
Great...welcome
Sibeesh VenuPosted Jun 29, 2015, 3:10 AM
Good one...
Mohan ArPosted Jun 29, 2015, 3:02 AM
Good One for stater..
Rajeesh MenothPosted Jun 29, 2015, 2:58 AM
Nice Start..Welcome Favas
Pankaj Kumar ChoudharyPosted Jun 29, 2015, 2:47 AM
Nice Start FAVAS Welcome to C#-Corner Community...........