Signup/Sign In

C# Constructors - Default, Parameterized, Copy, Private and Static Constructor

Posted in Technology   LAST UPDATED: AUGUST 25, 2021

    In this tutorial, we will learn about constructors and its different types. Also, the practical examples will help you to understand the concept of each constructor and its usage. When we create Class in C#, every class should have a constructor which is used to initialize the object of the class.

    Constructors are the special method of the class which is used to initialize the data members of the new object. It is associated with a class and get automatically invoked when the classes instance (i.e., objects) are created.

    Basic characteristics of constructors:

    1. The constructor of a class must have the same name as the class in which it resides

    2. A class can have any number of constructors

    3. A constructor can never be abstract, final, static and synchronized

    4. A constructor doesn't have a return type but neither the return type is void

    5. Constructors can have access modifiers along with it to control its access i.e. which other class can call the constructor


    Types of Constructors in C#

    1. Default Constructor

    2. Parameterized Constructor

    3. Copy Constructor

    4. Private Constructor

    5. Static Constructor

    Let's cover each one of them one by one.


    1. Default Constructor

    A default constructor is parameterless. If a class doesn't have a constructor then a default constructor gets called when object is created. The default constructor is added to a class by default if you don't add any constructor to your class. The default constructor should have public access.

    Filename: Program.cs (Example of default constructor)

    using System;
    
    namespace Studytonight
    {
        public class Student
        {
            public string name;
            public string ID;
            public int roll_no;
            public Student()
            {
                Console.WriteLine("Default Constructor Invoked!");
                name = "John Ryno";
                ID = "MBA58955";
                roll_no = 859;
            }
        }
    
        public class Program
        {
            public static void Main()
            {
                Student s = new Student();
                Console.WriteLine(s.name + "\n" + s.ID + "\n" + s.roll_no);
            }
        }
    }

    Output:

    Default Constructor Invoked!
    John Ryno
    MBA58955
    859

    In the code example above, we have specified the default constructor ourselves, but even if we don't do that, the compiler will automatically assign a default contructor to the class which is used for initialization of object of the class.


    2. Parameterized Constructor

    A constructor with at least one parameter is called as parameterized constructor.

    Filename: Program.cs (Example of the parameterized constructor)

    using System;
    
    namespace Studytonight
    {
        public class Student
        {
            public string name;
            public string ID;
            public int roll_no;
            public Student(string n, string id, int rno)
            {
                name = n;
                ID = id;
                roll_no = rno;
                Console.WriteLine(name + "\n" + id + "\n" + roll_no);
            }
        }
    
        public class Program
        {
            public static void Main()
            {
                Student s1 = new Student("John Ryno", "MBA58955", 859);
                s1 = new Student("John Ryno", "MBA58955", 1577);
                Console.WriteLine();
            }
        }
    }

    Output:

    John Ryno
    MBA58955
    859
    John Ryno
    MBA58955
    1577


    3. Copy Constructor

    The copy constructor is used to create an object by copying all of its variables(attributes) from another object. This type of constructors are used for initializing a new instance from an existing one.

    Filename: Program.cs (Example of the copy constructor)

    using System;
    
    namespace Studytonight
    {
        public class Student
        {
            public string name;
            public string ID;
            public int roll_no;
            public Student(string n, string id, int rno)
            {
                name = n;
                ID = id;
                roll_no = rno;
            }
    
            // Copy Constructor
            public Student(Student s)
            {
                name = s.name;
                ID = s.ID;
                roll_no = s.roll_no;
            }
        }
    
        public class Program
        {
            public static void Main()
            {
                Student s1 = new Student("John Ryno", "MBA58955", 859);
                Console.WriteLine("s1 is: " + s1.name + "\n" + s1.ID + "\n" + s1.roll_no);
                // Another student by copying student details
                Student s2 = new Student(s1);
                Console.WriteLine("s2 is: " + s2.name + "\n" + s2.ID + "\n" + s2.roll_no);
                s2.name = "Dyna Winta";
                s2.ID = "PHRMA96665";
                s2.roll_no = 122;
                Console.WriteLine("s2 is: " + s2.name + "\n" + s2.ID + "\n" + s2.roll_no);
            }
        }
    }

    Output:

    s1 is: John Ryno
    MBA58955
    859
    s2 is: John Ryno
    MBA58955
    859
    s2 is: Dyna Winta
    PHRMA96665
    122


    4. Private Constructor

    A constructor that is preceded by a private access modifier is called a private constructor. It does not make it possible for other classes to inherit any data from this class (we can't instantiate the class if the constructor is private). We will cover more on this concept when we will cover the concept of inheritance.

    Following are its characteristics:

    1. Generally, private constructor is used in classes that contain static members only.

    2. We can't create public and private constructors simultaneously in a class, both without parameters.

    3. We can't instantiate the class with a private constructor.

    4. If we want to create an object of a class with private constructor then, we need to have public constructor along with it

    Filename: Program.cs (Example of the private constructor)

    using System;
    
    namespace Studytonight
    {
        public class Student
        {
            public int roll_no;
            private Student()
            {
                Console.WriteLine("Private Constructor");
            }
    
            public Student(int rno)
            {
                roll_no = rno;
            }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                // Student s1 = new Student(); // it will generate an error because the constructor is inaccessible
                Student s1 = new Student(4559);
                Console.WriteLine(s1.roll_no);
            }
        }
    }

    Output:

    4559


    5. Static Constructor

    This type of constructor will be invoked only once for all the instances of the class and it is invoked during the creation of the first instance of the class.

    Following are its characteristics:

    1. Static constructor neither accepts parameters nor access modifiers.

    2. In a class, only one static constructor is allowed.

    3. Static constructor will invoke automatically, whenever we create the first instance of a class.

    4. It is used to initialize static fields of the class.

    Filename: Program.cs (Example of the static constructor)

    using System;
    
    namespace Studytonight
    {
        public class Student
        {
            static Student()
            {
                Console.WriteLine("Static Constructor");
            }
    
            public Student()
            {
                Console.WriteLine("Default Constructor");
            }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Student s1 = new Student();
                Student s2 = new Student();
            }
        }
    }

    Output:

    Static Constructor
    Default Constructor
    Default Constructor

    If you observe the above example, we have created a static constructor as well as a default constructor. Here the static constructor will be invoked only once for the first instance of the class.

    In this article, we discussed various types of constructors in C# language, we hope you found this article useful.

    You may also like:

    Author:
    Subject Matter Expert of C# Programming at Studytonight.
    C#C# TutorialC# Constructor
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS