Constructor In C#

16:24

Introduction

To initialize the data member of a class in runtime that means when object is created. If we create any class without constructor, the compiler will automatically create one default constructor for that class. There is always at least one constructor in every class.

Definition

Constructor is a special type of method which name is the same as class name.
Constructor have no return type.
Constructor automatically call when object is created.
Constructor used to initialize data member of a class when object is created.
Example:
    class example
    {
        public example()
        {
            Console.WriteLine("Calling Constructor");
        }
    }

Types Of Constructor

Constructors can be classified into 5 types
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Static Constructor
5. Private Constructor

1. Default Constructor

A constructor without any parameters is called as default constructor. Every instance of the class will be initialized without any parameter values.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Constructor
{
    class Example
    {
        public string value1, value2;
        public Example()                                           // Default Constructor
        {
            value1 = "Welcome";
            value2 = "Coder 007";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Example obj = new Example();                           // Default Constructor Called
            Console.WriteLine(obj.value1);
            Console.WriteLine(obj.value2);
            Console.ReadLine();
        }
    }
}
Output:
    Welcome
    Coder 007

2. Parameterized Constructor

A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Constructor
{
    class Example
    {
        public string value1, value2;
        public Example(string a, string b)                        // Parameterized Constructor
        {
            value1 = a;
            value2 = b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Example obj = new Example("Welcome", "Coder 007");    // Parameterized Constructor Called
            Console.WriteLine("You are " + obj.value1 + " " + obj.value2);
            Console.ReadLine();
        }
    }
}
Output:
    You are Welcome Coder 007

3. Copy Constructor

A parameterized constructor that contains a parameter of same class type is called as copy constructor.
Main purpose of copy constructor is to initialize new instance to the values of an existing instance.
Used to make deep copy of objects.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Constructor
{
    class Example
    {
        public string value1, value2;

        public Example(string x, string y)
        {
            value1 = x;                                           // Deep Cell
            value2 = y;
        }

        public Example(Example obj)                               // Copy Constructor
        {
            value1 = obj.value1;
            value2 = obj.value2;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Example obj = new Example("Welcome", "Coder 007");    // Constructor called
            Example objCopy = new Example(obj);
            Console.WriteLine("You are " + objCopy.value1 + " " + objCopy.value2);
            Console.ReadLine();
        }
    }
}
Output:
    You are Welcome Coder 007

Types Of Copy in C#
There are two types of copy:
1. Shallow Copy: Shallow Copy copies value very small means the types bit by bit and the result is that both instances are cloned and the original will refer to the same object.
2. Deep Copy: Deep Copy is used to make a complete deep copy of the internal reference types.

4. Static Constructor

Static constructor will execute automatically whenever we create first instance of class
Static constructor will not accept any parameters because it is automatically called by CLR.
Can not use any parameter
Only one static constructor will allowed.
Static constructor will not have any access modifiers.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Constructor
{
    class Example
    {
        public string value1, value2;
        static Example()
        {
            Console.WriteLine("Static Constructor");
        }

        // Private is allowed but static not allowed
        //private static Example(int a)
        //{

        //}

        // Can not use more then one static constructor
        //static Example()
        //{
            
        //}

        public Example()
        {
            value1 = "Welcome";
            value2 = "Coder 007";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Here Both Static and instance constructors are invoked for first instance
            Example obj1 = new Example();
            Console.WriteLine(obj1.value1 + " " + obj1.value2);

            // Here only instance constructor will be invoked
            Example obj2 = new Example();
            Console.WriteLine(obj2.value1 + " " + obj2.value2);

            Console.ReadLine();
        }
    }
}

Output:
    Static Constructor
    Welcome Coder 007
    Welcome Coder 007

5. Private Constructor

One use of private constructor is when we have only static member.
Once we provide a constructor that is either private or public or any, the compiler will not allow us to add public constructor without parameters to the class.
If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor.
If we want to use that class, then other member of method must be static.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Constructor
{
    class Example
    {
        public string value1, value2;
        public Example(string a, string b)
        {
            value1 = a;
            value2 = b;
        }

        private Example()                                    // Private Constructor
        {
            Console.WriteLine("Private Constructor");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // If I comment public method and there have one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances below.
            Example obj1 = new Example("Welcome", "Coder 007");
            Console.WriteLine(obj1.value1 + " " + obj1.value2);

            Console.ReadLine();
        }
    }
}

Output:
    Welcome Coder 007

Constructor Overloading

If we creating another constructor with same method name and different parameters then its called Constructor Overloading.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Constructor
{
    class Example
    {
        public string value1, value2;

        public Example()
        {
            value1 = "Welcome";
            value2 = "Coder 007";
        }

        public Example(string a, string b)                        // Constructor Overloading
        {
            value1 = a;
            value2 = b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Example obj1 = new Example();
            Console.WriteLine(obj1.value1 + "" + obj1.value2);

            Example obj2 = new Example("Constructor", "Overloading");
            Console.WriteLine("This is " + obj2.value1 + " " + obj2.value2);

            Console.ReadLine();
        }
    }
}
Output:
    Welcome Coder 007
    This is Constructor Overloading

Conclusion

I explain about Constructor and its all types hope its very helpful.

You Might Also Like

0 comments