Constructor in C#

0
Constructor in C#:- 


It is a special member function of a class that will be called when we create an object.  The constructor name and Class name both will be the same but the constructor has no return type.

Constructor is used to initializing the dynamic data member of the class when we create the object.

When we compile any class of C# then the constructor block will be created automatically.



Type of  Constructor:-

1)  Static:- 

This Constructor will be called automatically without creating an object. It is used to provide the initialization to static data members of the class.

Example of Static Constructor:-

 class ConstExample
    {
        int a = 100, b = 200, c;
        static ConsExample()  //static constructor
        {
            Console.WriteLine("Static Constructor");
        }
        public static void Main()
        {

         }
}

Another Example of Static Constructor:-

class ConstructorExample
    {
        static int x, y;
        static ConstructorExample()
        {
            x = 10;
            y = 20;
            Console.WriteLine(x + y);
        }
        static void Main()
        {
            Console.WriteLine("Main");
            Console.ReadKey();
        }

    }

note:-  Static Constructor always will be the default, it can not be parameterized.

2) Dynamic:-  This Constructor will be called by creating an object and it is used to initialize the dynamic data member of the class.

2.1)  Default  Constructor:-    We can not pass any value to the default constructor, It will be by default created called by Object.

class ConstExample
    {
        int a = 100, b = 200, c;
     
        ConstExample () //Default Constructor
        {
            c = a + b;
            Console.WriteLine(c);
        }
        public static void Main()
        {
           ConstExample obj = new ConstExample();
            Console.ReadKey();
        }
    }

2.2)  Parametrised  Constructor:-   

We can pass parameters under the constructor block using  Parametrised Constructor

class ConstExample
    {
        int a ,b, c;
        ConstExample (int a,int b) //Default Constructor
        {
            this.a=a;
           this.b=b;
        }
        public static void Main()
        {
           ConstExample obj = new ConstExample(100,200);
            Console.ReadKey();
        }
    }

2.3)  Copy  Constructor:- 
 

using this we can copy the value of one object into another object, copy constructor will be called when we pass the reference of the object.

class ConstrctoeExample
    {
        int a = 100, b = 200, c;
        static ConstrctoeExample()  //static constructor
        {
            Console.WriteLine("Static Constructor");
        }
        ConstrctoeExample()  //Default Constructor
        {
            a = 10;
            b = 2;
            c = a + b;
            Console.WriteLine(c);
        }
        ConstrctoeExample(int a,int b)  //Paramerised Constructor
        {
            this.a = a;
            this.b = b;
            c = a + b;
            Console.WriteLine(c);
        }
        ConstrctoeExample(ConstrctoeExample o)  //Copy Constructor
        {
            this.a = o.a;
            this.b = o.b;
            c = a - b;
            Console.WriteLine(c);
        }
        public static void Main()
        {
            ConstrctoeExample obj2 = new ConstrctoeExample();
           ConstrctoeExample obj = new ConstrctoeExample(100,20);
           ConstrctoeExample obj1 = new ConstrctoeExample(obj2);
            Console.ReadKey();
        }
    }

     
                           
Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)