articles

Home / DeveloperSection / Articles / Constructor and Its Types

Constructor and Its Types

Sushant Mishra5332 02-Mar-2017

Constructor is a member and special method of class. Constructor name must be same as 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. Constructor are responsible 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

4. Static Constructor

5. Private Constructor

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
{
    classSample
    {
        publicstring param1, param2;
        public Sample()     // Default Constructor
        {
            param1 = "Hello Guys";
            param2 = "It is Mindstick- Sushant";
        }
    }
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Sample obj = newSample();   // Once object of class created automatically constructor will be called
            Console.WriteLine(obj.param1);
            Console.WriteLine(obj.param2);
            Console.ReadLine();
        }
    }
}
Output:
Hello Guys
It 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 with same name and different parameter.

Example:
using System;
namespace ConsoleApplication3
{
    classSample
    {
        publicstring param1, param2;
        public Sample(string x, string y)     // Declaring Parameterized constructor with Parameters
        {
            param1 = x;
            param2 = y;
        }
    }
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Sample obj = newSample("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
{
    classSample
    {
        publicstring 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 Constructor
Hello 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
{
    classSample
    {
        publicstring 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;
        }
    }
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Sample obj = newSample("Hello Guys", "Here is Mindstick-Sushant"); // Create instance to class Sample
            Sample obj1 = newSample(obj); // Here obj details will copied to obj1
            Console.WriteLine(obj1.param1 + " , " + obj1.param2);
            Console.ReadLine();
        }
    }
}
Output:
This is Copy Constructor
Hi 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
{
    classSample
    {
        publicstring param1, param2;
        static Sample()
        {
            Console.WriteLine("Static Constructor");
        }
        public Sample()
        {
            param1 = "Sample";
            param2 = "Instance Constructor";
        }
    }
    classProgram
    {
        staticvoid Main(string[] args)
        {
            // Here Both Static and instance constructors are invoked for first instance
            Sample obj = newSample();
            Console.WriteLine(obj.param1 + " " + obj.param2);
            // Here only instance constructor will be invoked
            Sample obj1 = newSample();
            Console.WriteLine(obj1.param1 + " " + obj1.param2);
            Console.ReadLine();
        }
    }
}
Output:
Static Constructor
Sample Instance Constructor
Sample 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 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
{
    publicclassSample
    {
        publicstring param1, param2;
        public Sample(string a, string b)
        {
            param1 = a;
            param2 = b;
        }
        private Sample()  // Private Constructor Declaration
        {
            Console.WriteLine("Private Constructor with no prameters");
        }
    }
    classProgram
    {
        staticvoid Main(string[] args)
        {
            // Here we don't have chance to create instace for private constructor
            Sample obj = newSample("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

What is Constructor in c#

Using Generics in C#


Updated 31-Mar-2019

Leave Comment

Comments

Liked By