Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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.

Type of  Constructor:-

1)  Static:- 

This Constructor will be called automatically without creating an object .
 class ConstExample
    {
        int a = 100, b = 200, c;
        static ConsExample()  //static constructor
        {
            Console.WriteLine("Static Constructor");
        }
public static void Main()
        {
         }
}


2) Dynamic:-  This Constructor will be called by creating an object

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.

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)  //Default 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();
        }
    }

     
                           

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post