blog

Home / DeveloperSection / Blogs / Constructor in C#.Net

Constructor in C#.Net

Sachindra Singh6431 17-Jan-2011

Constructor is a special type of method that is use to initialize member variable of class, Constructor does not have any return type, Construtctor have the same name of the class. We write the lines of code in constructor of a class which we want to be executed as soon as the instance or object of the class is created.

Kinds of Constructor:

1.       Instance or Default Constructor

2.       Static Constructor

3.       Parameterized Constructors

1.     Instance or Default Constructor:

 Constructor called implicitly when we create the class instance, the modifiers can be public, private, protected, and internal or protected internal.

using System; 
class XYZ
{
      int  num1,num2,res;//Member Variables
      public  XYZ()//Creating Constructor with class  name
      {
        num1 = 10;// Initializing  member variable in constructor
        num2=20;// Initializing  member variable in constructor
      }
      public  void  Sum()
      {
            res=num1+num2;//Adding value of variables and store in another variable
            Console.WriteLine("The sum of number is-->{0}",res);//This line display value of  res(30)
      }
      public static void Main(String []args)
      {
            XYZ obj=new XYZ();//new  is  a key word that create the class instance
            //obj is a class variable that store reference of the class 
            obj.Sum();
      }
}                                                                                                                                  


2.     Static Constructor:

This is a new concept introduced in C#, static Constructor will call only once when the class gets loaded in the memory without creating the instance of the class. It can only access the static members of the class. There can be only one static constructor in the class. The static constructor should be without parameters.

using System; 
 
public class ABC
{
    static int num1 = 20, num2 = 30, res;
    static ABC()
    {
        res = num1 * num2;
        Console.WriteLine("The product of values is -->{0}", res);//This line display value of res(600)
    }
  
    public static void Main(String[] args)
    {
      /*static Constructor call without creating the insatnce of the class
       but only static variable can intialize in static Constructor*/
    }
}
 


3.     Parameterized Constructors:

At times, we will require initializing class members during instantiation and this is the time where parameterized constructor will come into picture. It follows the same rules as default constructor and will have parameters  . Go through following snippet:

 

public class Name 
{
    public Name()
    {
        //A default Constructor
    }
 
    public Name(String strName)
    {
        //A parameterized Constructor having one parameter
        Console.WriteLine("Name is -->" + strName);
    }
 
    public Name(String strFirstName, String strLastName)
    {
        //A parameterized Constructor having two parameters
        Console.WriteLine(" First name is -->" + strFirstName+"  and Last name is-->"+strLastName);
    }
 
    public static void Main(String[] args)
    {
        Name obj = new Name("Sachindra");//It call that Constructor having one parameter
        Name obj1 = new Name("Sachinindra","Kumar");//Creating new instance of the class and it call that Constructor having two parameters
    }
 
}


Updated 18-Sep-2014

Leave Comment

Comments

Liked By