In this blog, I am going to explain the concept of Constructor Chaining In C#. This detailed blog will cover the following topics:
- Introduction
- Constructors in C#
- Constructor Chaining In C#
- Benefits of Constructor Chaining
- Drawbacks of Constructor Chaining
- Conclusion
Constructors in C#
As per Microsoft, "When a class or struct is instantiated, its constructor is called. Constructors have the same name as the class or struct, and they usually initialize the data members of the new object".
As per Wikipedia, "In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables".
In simple words, a constructor is a special type of method that is automatically invoked when an instance of a class is created. Constructors are mostly used to initialize the private fields of a class while an instance of the class is being created.
Key Points
- A class can have any number of constructors.
- Constructors can also be overloaded.
- A constructor doesn't have any return type, not even void.
- Within a class, you can create one static constructor only.
- The compiler will automatically create a default constructor in a class when there is no constructor in the class.
Example
public class ConstructorInCSharp
{
public bool IsInitialized;
public ConstructorInCSharp()
{
IsInitialized = true;
}
}
class Program
{
static void Main()
{
ConstructorInCSharp myObj = new ConstructorInCSharp();
Console.WriteLine(myObj.IsInitialized);
Console.ReadLine();
}
}
Constructor Chaining in C#
In simple words, calling one constructor from another constructor is called constructor chaining in C#.
Syntax
public mySampleClass(): this() //Calls another constructor of the same class
public mySampleClass(): base() //Calls the base class constructor
1) Calling the same class constructor
A constructor can call another constructor in the same class using "this" keyword. The "this" keyword can be used with or without parameters. Any parameters in the constructor are available as parameters to "this" or as part of the expression.
Example
internal class ConstructorChainingInCSharp
{
public string LoginId;
public int EmployerId;
public ConstructorChainingInCSharp() : this("OnkarSharmaMVP", 228384)
{
Console.WriteLine("\n!! This is default constructor !!");
}
public ConstructorChainingInCSharp(string LoginId, int EmployerId)
{
this.LoginId = LoginId;
this.EmployerId = EmployerId;
Console.WriteLine("LoginId: {0} , EmployerId: {1}", LoginId, EmployerId);
}
static void Main(string[] args)
{
ConstructorChainingInCSharp obj = new ConstructorChainingInCSharp();
Console.ReadLine();
}
}
2) Calling the base class constructor
The "base" keyword can be used in a constructor to call the constructor of the base class. The "base" keyword can be used with or without parameters. Any of the parameters of the constructor can be used as parameters of the base, or as part of an expression.
Example
public class Employee
{
public string LoginId;
public int EmployerId;
public Employee(string LoginId, int EmployerId)
{
this.LoginId = LoginId;
this.EmployerId = EmployerId;
Console.WriteLine("LoginId: {0} , EmployerId: {1}", LoginId, EmployerId);
}
}
public class Manager : Employee
{
public Manager() : base("OnkarSharmaMVP", 228496)
{
Console.WriteLine("\n!! This is default constructor !!");
}
public static void Main(string[] args)
{
Manager obj = new Manager();
Console.ReadLine();
}
}
Benefits of Constructor Chaining in C#
- It supports inheritance with constructor chaining.
- Proper use of constructor chaining allows you to avoid redundancy by reusing constructor logic.
Drawbacks of Constructor Chaining in C#
Constructor chaining also has some drawbacks that you should consider before using it. Let's look at some of them:
- This can lead to increased complexity and confusion in the order and execution of constructors.
- Improper use of chaining can lead to increased code duplication. If the constructor is not designed carefully.
- This can lead to a tight coupling between multiple constructors in the same class. This means that modifications to one constructor can affect others.
See you in the next blog, till then, take care and be happy learning.
You can connect with me @
Reference: https://learn.microsoft.com/
Conclusion
In this blog, we have discussed the concept of Constructor Chaining In C#.
I hope you enjoyed this blog. Follow C# Corner to learn more new and amazing things about C#.
Thanks for reading.