How many types of constructors are in C#, why create the private constructor in the C# program explain with an example.

0 votes
61 views
Ethan Karla asked 24 days ago in programming by Ethan Karla

How many types of constructors are in C#, why create the private constructor in the C# program explain with an example.

1 Answer

0 votes
Ravi Vishwakarma answered 18 days ago by Ravi Vishwakarma

Constructors are used to initializing the values of the data members of the class. The constructor is a special member function of the class. The name of the class is the same as the name of the constructors. Constructors do not have a return type. When the object of the class is created, the constructor is called automatically. It is never virtual. These are declared in the public section. It is not inherited by the derived class. When we do not provide any constructors in the program, then the compiler generates the default constructor for us.

Syntax 
[ access modifier ] class-name( [ arguments1, arguments2, .... ] )
{
    // constructor statement 
}

There are many types of constructor

  1. default constructor
  2. parameterized constructor
  3. copy constructor
  4. static constructor
  5. private constructor
  6. public constructor

default constructor

When we do not provide any constructors in the class program, then the compiler generates the default constructor for us. it has no body, it is always used the public access modifier and it has not taken any arguments and no return value.

public class-name()
{
}

parameterized constructor

this is an explicitly defined constructor and this constructor has taken one or more parameters.

public class-name( arguments1, arguments2, ... )

{
}

copy constructor

this is an also explicitly defined constructor and this constructor has taken object type one or more parameters.

public class-name( class-name object1, class-name object2, ... )

{
}

static constructor

this is an also explicitly defined constructor and this constructor doesn't take any arguments, this constructor is the same as the default constructor but the static constructor is invoked only one time in a program but, What is the default constructor that the number of times we will create an object of the class, the default constructor will be called. it also creates a static class. 

public static class-name()

{
}

private constructor

in the class don't use the access modifier before the constructor then it is called the private constructor, by default constructor is private. it is accessible in the same class.

class-name( [ arguments1, arguments2, .... ] )
{
}

public constructor

by default constructor is private, so out of class doesn't access then we need to make the public constructor, it is accessible in the same and out of class.

public class-name( [ arguments1, arguments2, .... ] )

{
}

Example 

using System;

namespace ConstructorDemo
{
    class Program
    {
        // static constructor
        static Program()
        {
            Console.WriteLine('It is a static constructor, i am invoked only one time');
        }
        // private constructor
        Program()
        {
            Console.WriteLine('i am by default private constructor');
        }
        static void Main(string[] args)
        {
            // calling the static and private constructor
            Program program = new Program();
            // calling the private constructor
            Program program1 = new Program();
            // calling the default constructor
            DemoDemo demo = new DemoDemo();
            // calling the parameterized constructor
            DemoDemo demo1 = new DemoDemo(12);
            // calling the copy constructor
            DemoDemo demo2 = new DemoDemo(demo1);
        }
    }
    class DemoDemo
    {
        private int para = 0;
        static DemoDemo()
        {
            Console.WriteLine('It is a static constructor, i am invoked only one time');
        }
        // public default constructor
        public DemoDemo()
        {
            Console.WriteLine('It is a public constructor');
        }
        // parameterized constructor
        public DemoDemo(int parameter)
        {
            this.para = parameter;
            Console.WriteLine($'It is a parameterize constructor {parameter}');
        }
        // copy constructor
        public DemoDemo(DemoDemo parameter)
        {
            Console.WriteLine($'It is a copy constructor {parameter.para}');
        }
    }
}

output:

How many types of constructors are in C#, why create the private constructor in the C# program explain with an example.