Working With Constructor in C#

Introduction

A constructor is used to initialize class fields. A class constructor is automatically called when an instance of a class is created. Whenever a class or struct is created, its constructor is called. 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 as listed in Default Values Table (C# Reference).
  • 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.

  1. class CoOrds  
  2. {  
  3.     public int x, y;  
  4.     // default constructor  
  5.     public CoOrds()  
  6.     {  
  7.         x = 0;  
  8.         y = 0;  
  9.     }  
  10. }  

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. 

  1. // 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:

  1. 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.

 

  1. class SimpleClass  
  2. {  
  3.     // Static variable that must be initialized at run time.  
  4.     static readonly long baseline;  
  5.     // Static constructor is called at most one time, before any  
  6.     // instance constructor is invoked or member is accessed.  
  7.     static SimpleClass()  
  8.     {  
  9.         baseline = DateTime.Now.Ticks;  
  10.     }  
  11. }  

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.

 

  1. class NLog  
  2. {  
  3.     // Private Constructor:  
  4.     private NLog() {}  
  5.     public static double e = Math.E; //2.71828...  
  6. }