Use of Constructor in C#

Constructor Introduction

  • The Constructor is used to initialize the object of the class.
  • The Constructor name is the same as the class Name.
  • The Constructor should be public, private and protected etc.
  • A Constructor can be overloaded.

Types of Constructors

  • Default Constructor
  • Parameterized Constructor
  • Private Constructor
  • Copy Constructor
  • Static Constructor

1. Default Constructor

Example

namespace ConstructorExample

{

    class DefaultConstructor

    {

        int a, b;

        public DefaultConstructor()

        {

            a = 23;

            b = 56;

 

        }

        public void display()

        {

            Console.WriteLine("sum="+(a+b));

        }

    }

    class Program

    {

        public static void Main()

        {

            DefaultConstructor dc = new DefaultConstructor();

            dc.display();

            Console.ReadLine();

        }

    }

}

 

2. Parameterized Constructor

Example

namespace ConstructorExample

{

    class ParameterizeConstructor

    {

        int a, b;

        public ParameterizeConstructor(int a, int b)

        {

            this.a =a;

            this.b =b;

 

        }

        public void display()

        {

            Console.WriteLine("sum="+(a+b));

        }

    }

    class Program

    {

        public static void Main()

        {

            ParameterizeConstructor pc = new ParameterizeConstructor(12, 32);

            pc.display();

            Console.ReadLine();

        }

    }

}

 

3. Private Constructor

Example


namespace ConstructorExample

{

    class PrivateConstructor

    {

       static int a, b;

        public PrivateConstructor(int x, int y)

        {

            a =x;

           b =y;

 

        }

        //create this function because private constructor is not nilitilize in another class

        public static PrivateConstructor InitilizeConstructor()

        {

            return new PrivateConstructor(12, 30);

        }

        public static void display()

        {

            Console.WriteLine("sum="+(a+b));

        }

    }

    class Program

    {

        public static void Main()

        {

            PrivateConstructor.InitilizeConstructor();  

           PrivateConstructor.display();

            Console.ReadLine();

        }

    }

}

 

4. Copy Constructor

Example

namespace ConstructorExample

{

    class CopyConstructor

    {

        int a, b,x,y;

       public CopyConstructor(int x, int y)

        {

            a =x;

            b =y;

 

        }

       

       public CopyConstructor( CopyConstructor c) 

        {

            x = c.a;

            y = c.b;

            

        }

        public void display()

        {

            Console.WriteLine("valuof a=" + a + "\n b=" + b);

        }

        public void Show()

        {

            Console.WriteLine("valuof x=" +x+"\n y="+y);

        }

    }

    class Program

    {

        public static void Main()

        {

            CopyConstructor cc = new CopyConstructor(12,30);

            cc.display();

            CopyConstructor cc2 = new CopyConstructor(cc);

            cc2.Show();

            Console.ReadLine();

        }

    }

}

 

5. Static Constructor

Example

namespace ConstructorExample

{

    class StaticConstructor

    {

        static string name;

        static int age;

        static StaticConstructor()

        {

            Console.WriteLine("Static constructor Initilize");

            name = "Deepak";

            age = 24;

        }

       

      

        public static void display()

        {

            Console.WriteLine("Name=" + name + "\nAge=" + age);

        }

       

    }

    class Program

    {

        public static void Main()

        {

            StaticConstructor.display();

            Console.ReadLine();

        }

    }

}

Ebook Download
View all
Learn
View all