---
title: "Constructor in C#.Net"  
description: "Constructor is a special type of method that is use to initialize member variable of class, Constructor does not have any return type, Construtctor ha"  
author: "Sachindra Singh"  
published: 2011-01-17  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/98/constructor-in-c-sharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Constructor in C#.Net

Constructor is a special type of method that is use to initialize member [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) 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](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code) or object of the class is created.

##### Kinds of Constructor:

1. Instance or [Default Constructor](https://www.mindstick.com/forum/34531/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](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers), protected, and [internal](https://www.mindstick.com/interview/773/describe-the-accessibility-modifier-protected-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](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) 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](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) the static members of the class. There can be only one static constructor in the class. The static constructor should be without [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp).

```
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](https://answers.mindstick.com/qa/109507/how-to-set-bing-as-default-seek-engine) 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     }  }
```

---

Original Source: https://www.mindstick.com/blog/98/constructor-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
