---
title: "Constructor and Its Types"  
description: "In this article , we are discussing constructor and its types , where and how to use them using C# console programming."  
author: "Sushant Mishra"  
published: 2017-03-02  
updated: 2019-03-31  
canonical: https://www.mindstick.com/articles/12254/constructor-and-its-types  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# Constructor and Its Types

Constructor is a member and special method of class. Constructor name must be same as [Class name](https://www.mindstick.com/forum/12871/how-to-get-class-name). Constructor method never return anything, so return type of Constructor never used. It invokes automatically when we create instances of a class. If we not create a constructor of a class then compiler will automatically define one [default constructor](https://www.mindstick.com/forum/34531/default-constructor). Constructor are [responsible](https://answers.mindstick.com/qa/94149/who-is-one-of-the-responsible-person-for-the-department-of-space-dos) to create object and allocate memory location of its class.A class have also a destructor in which is used for manual cleaning of memory which occupied from Class.\

##### Types of Constructor:

1 .Default Constructor

2. Parameterized Constructor

3. [Copy Constructor](https://www.mindstick.com/blog/170/copy-constructor-in-c-sharp)

4. Static Constructor

5. [Private Constructor](https://www.mindstick.com/interview/1949/where-and-how-can-you-use-a-private-constructor-in-java)

**Default Constructor**: A Constructor which have not any parameter is called by Default Constructor. This constructor create instance of class which have no parameter value.

##### Example

```
using System;namespace ConsoleApplication3{    class Sample    {        public string param1, param2;        public Sample()     // Default Constructor        {            param1 = "Hello Guys";            param2 = "It is Mindstick- Sushant";        }    }    class Program    {        static void Main(string[] args)        {            Sample obj = new Sample();   // Once object of class created automatically constructor will be called            Console.WriteLine(obj.param1);            Console.WriteLine(obj.param2);            Console.ReadLine();        }    }}
```

##### Output:

```
Hello GuysIt is MindStick-Sushant
```

\

**Parameterized Constructor**: A constructor which have at least on parameter in its defination is called as Parametrized Constructor. This constructor create instances of class with different value each time.In oops based language, we can use [overloaded constructor](https://www.mindstick.com/forum/159858/when-to-use-overloaded-constructor) with same name and different parameter.

##### Example:

```
using System;namespace ConsoleApplication3{    class Sample    {        public string param1, param2;        public Sample(string x, string y)     // Declaring Parameterized constructor with Parameters        {            param1 = x;            param2 = y;        }    }    class Program    {        static void Main(string[] args)        {            Sample obj = new Sample("Hello Guys","Here is Mindstick- Sushant");   // Parameterized Constructor Called            Console.WriteLine(obj.param1 + "  " + obj.param2);            Console.ReadLine();        }    }}
```

##### Output

```
Hello Guys Here is Mindstick-Sushant
```

##### Overloaded Parameterized Constructor Example

```
using System;namespace ConsoleApplication3{    class Sample    {        public string param1, param2;         public Sample()     // Default Constructor        {            param1 = "Hi";            param2 = "I am Default Constructor";        }        public Sample(string x, string y)     // Declaring Parameterized constructor with Parameters        {            param1 = x;            param2 = y;        }    }    class Program    {        static void Main(string[] args)        {            Sample obj = new Sample();   // Default Constructor will Called            Sample obj1 = new Sample(("Hello Guys", "Here is Mindstick- Sushant");   // Parameterized Constructor will Called            Console.WriteLine(obj.param1 + ", " + obj.param2);            Console.WriteLine(obj1.param1 + ", " + obj1.param2);            Console.ReadLine();        }    }}
```

##### Output:

```
Hi, I am Default ConstructorHello Guys, Here is Mindstick- Sushant
```

**Copy Constructor:** Copy constructor is type of parameterized constructor with parameter of class object. Copy Constructor is used to create new instance of an existing instance. So it is used for creating another instance of the class with the existing instance.

##### Example :

```
using System;namespace ConsoleApplication3{    class Sample    {        public string param1, param2;        public Sample(string x, string y)        {            param1 = x;            param2 = y;        }        public Sample(Sample obj)     // Copy Constructor        {            Console.WriteLine("This is Copy Constructor");            param1 = obj.param1;            param2 = obj.param2;        }    }    class Program    {        static void Main(string[] args)        {            Sample obj = new Sample("Hello Guys", "Here is Mindstick-Sushant");
//
Create instance to class Sample            Sample obj1 = new Sample(obj); // Here obj details will copied to obj1            Console.WriteLine(obj1.param1 + " , " + obj1.param2);            Console.ReadLine();        }    }}
```

##### Output:

```
This is Copy ConstructorHi Guys, Here is Mindstick-Sushant
```

**Static Constructor:** Static Constructor is invoked only once for any number of instances. It invokes only on the first instance of class or first reference to static member in class. Static constructor is used to initialize static fields or attribute of class and to write the code which needs to execute only once.Static Constructor will not accept any parameter, it is automatically called by CLR and only one static constructor is allowed in a class.

##### Example:

```
using System;namespace ConsoleApplication3{    class Sample    {        public string param1, param2;        static Sample()        {            Console.WriteLine("Static Constructor");        }        public Sample()        {            param1 = "Sample";            param2 = "Instance Constructor";        }    }    class Program    {        static void Main(string[] args)        {            // Here Both Static and instance constructors are invoked for first instance            Sample obj = new Sample();            Console.WriteLine(obj.param1 + " " + obj.param2);            // Here only instance constructor will be invoked            Sample obj1 = new Sample();            Console.WriteLine(obj1.param1 + " " + obj1.param2);            Console.ReadLine();        }    }}
```

##### Output:

```
Static ConstructorSample Instance ConstructorSample Instance Constructor
```

**Private Constructor:** Private Constructor is also called as Special Instance Constructor. It is used only for that class which have only static members. A class that have one or more private constructor is not allowed to initialize, it means that class can neither be create objects nor inherited. If we want to create to object of that class we must create at least one public parameterized constructor.Main purpose of private constructor is to stop creating object of a class. It means a class that have only [private constructors](https://www.mindstick.com/interview/2338/private-constructors-can-be-used-in-the-singleton-design-pattern) are not allowed to create object and also not to be inherited.\
Main purpose of private constructor is to stop creating object of a class. It means a class that have only private constructors are not allowed to create object and also not to be inherited.

##### Example

```
using System;namespace ConsoleApplication3{    public class Sample    {        public string param1, param2;        public Sample(string a, string b)        {            param1 = a;            param2 = b;        }        private Sample()  // Private Constructor Declaration        {            Console.WriteLine("Private Constructor with no prameters");        }    }    class Program    {        static void Main(string[] args)        {            // Here we don't have chance to create instace for private constructor            Sample obj = new Sample("Hello Guys", ", Here is Mindstick-Sushant");            Console.WriteLine(obj.param1 + " " + obj.param2);            Console.ReadLine();        }    }}
```

##### Output:

Hello Guys ,Here is Mindstick-Sushant

##### You can visit these related post

[Constructor in C#.Net](https://www.mindstick.com/blog/98/constructor-in-c-sharp-dot-net)

[What is Constructor in c#](https://www.mindstick.com/forum/33671/what-is-constructor-in-c-sharp)

[Using Generics in C#](https://www.mindstick.com/Articles/11968/c-sharp/using-generics-in-c-sharp)\

---

Original Source: https://www.mindstick.com/articles/12254/constructor-and-its-types

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
