In this article, we will discuss Constructors in C# and different types of constructors. More importantly, we will discuss these constructors’ differences and use cases.
Let’s start.
What Is a Constructor?
A Constructor is a unique method in a class that we use to generate the instance of a class. The name of a constructor is the same as the name of the class it is part of. Constructors can be public or private and donāt have a return type.
We mainly use constructors are used to initialize the properties and fields of a class with a set of values.
What Are the Different Types of Constructors?
The constructors in C# are broadly defined into 5 types:
- Default and Parameterless Constructors
- Parameterized Constructors
- Private Constructors
- Static Constructors
- Copy Constructors
Let’s take a look at all 5 types of constructors and their use cases along with a working example.
Default and Parameterless Constructor
A default constructor does not take any parameters. A default constructor always initializes the members of the class with the same default values every time an object is created.
We can also create our own parameterless constructor in a class:
public class Person { public string Name {get; set; } public int Age {get; set; } public Person() { Name = "testName"; Age = 25; } public void Display() { Console.WriteLine(Name + " " + Age); } }
Now, we can call this constructor to create an object instance:
var person1 = new Person(); person1.Display();
Even without our parameterless constructor, this caller code would be the same. C# generates a default constructor for us.
Since we are not using arguments, the CLR invokes the parameterless constructor and assigns default values mentioned in the constructor:
testName 25
In C#, all classes have a default constructor. If we don’t explicitly define a constructor in the class, CLR will assign a default constructor with default values for all member types.
Parameterised Constructor
As the name suggests, the parameterized constructor is a constructor with parameters. We use a parameterized constructor when we want to initialize different objects with different values:
public class Person { publicĀ stringĀ NameĀ {get;Ā set;Ā }Ā public int Age {get; set; } public Person(string name) { Name = name; } }
As a result, it now assigns the value passed as the parameter to the name of the person. If a parameterized constructor omits any field/property in the class, CLR assigns it a default value:
var person2 = new Person("John Doe"); person2.Display();
This will now assign the string to the Name
property and assign the default value 0
to the Age
property:
John Doe 0
Private Constructor
A private constructor is a constructor with a private access modifier. Other classes cannot directly use a private constructor during object invocation. However, private constructors are widely used in the Singleton design pattern. Let’s define a simple class that uses a singleton design pattern:
public class SingletonClass { private static SingletonClass _singletonClass; private SingletonClass() { } public static SingletonClass GetInstance() { if(_singletonClass is null) { _singletonClass = new SingletonClass(); } return _singletonClass; } }
As a result, if we instantiate a new member with the private constructor, it will throw an exception. Instead, we can access it only using the public access method:
//This will throw error //SingletonClass singletonClass = new SingletonClass(); var singletonClass = SingletonClass.GetInstance();
Static Constructor
Static constructors are special types of constructors in C# used for initializing static data in a class. We cannot invoke a static constructor while creating an instance. Instead, a CLR invokes a static constructor before the creation of the first instance or accessing static data.
Let’s look at a basic example of the static constructor:
public class Person { ... private static int studentCount; static Person() { studentCount = -1; } }
Furthermore, there are a few points to remember with Static constructors
- A static constructor doesn’t take access modifiers or have parameters
- A class or struct can only have one static constructor
- We cannot invoke a static constructor through code. A CLR invokes a static constructor automatically
- If we don’t provide a static constructor to initialize static fields, all static fields are initialized to their default value as listed in the Default values of C# types
Copy Constructor
A copy constructor is a constructor that takes an instance of its class as an argument. We use a copy constructor when we want to clone an object.
Let’s create a copy constructor that copies the values of one instance to another instance:
public class Person { publicĀ stringĀ NameĀ {get;Ā set;Ā }Ā public int Age {get; set; } public Person(Person student) { Name = student.Name; Age = student.Age; } }
If we now invoke the copy constructor using another instance of the same class, the new instance will have the properties with the exact values:
var person2 = new Person("John Doe"); var copyPerson = new Person(person2); copyPerson.Display();
As a result, this will now invoke the copy constructor and copy the existing values to the new instance:
John Doe 0
Constructor Overloading
A class can have multiple constructors with the same name and different sets of parameters. The CLR determines the constructor during the runtime based on the arguments we pass. To learn more about Overloading in C#, you can read our Polymorphism article.
Let’s reuse the same example and see how constructor overloading works:
public class Person { publicĀ stringĀ NameĀ {get;Ā set;Ā } public int Age {get; set; } public Person() { Name = "testName"; Age = 25; } public Person(string name) { Name = name; } }
Constructor Chaining
In a class where we have constructor overloading, we can implement constructor chaining. In the case of a constructor chaining, we can make a call to one constructor from another constructor.Ā
Let’s define an example that illustrates the use of constructor chaining and the chain of invocation:
public class Person { publicĀ stringĀ NameĀ {get;Ā set;Ā }Ā public int Age {get; set; } public Person(string name) { Name = name; } public Person(string name, int age) : this(name) { Age = age; } }
When we call the second constructor (with two parameters), it will first call the constructor with a single name
parameter. That’s because we are using the this
keyword on the second constructor and passing the name as an argument. After the execution of the first constructor, the code will reach the body of the one we called.
Constructors in Inheritance
As an object-oriented programming language, C# supports inheritance.
We can invoke the constructor of a base class using the base
keyword with the constructors of the child class:
public class Student: Person { public string Department; public Student(string name, int age, string department) : base(name, age) { Department = department; } public void Print() { base.Display(); Console.WriteLine("department is:" + department); } }
Now let’s use the above constructor to create an instance of the child class:
var student = new Student("John Doe", 30, "IT"); student.Print();
As a result, this will invoke the constructor of the parent class person first followed by the student constructor:
John Doe 30 department is:IT
Conclusion
In this article, we have learned a lot about constructors in C#. We’ve seen different constructor types and use cases for each one.Ā