How to call Constructor from another Constructor with same class in C# ?

Calling Constructor from another Constructor with same class

There are two ways to do the above :
1. Use this keyword to acheive this . Example below :
 public class Employee
{
    public Employee(): this(10)
    {
        // This is the no parameter constructor method.
        // First Constructor
    }
    public Employee(int Age)
    {
        // This is the constructor with one parameter.
        // Second Constructor
    }
}
So first constructor will can second constructior using this keyword
2.
public class Employee
{
    public Employee()
    {
        Employee(10)
        // This is the no parameter constructor method.
        // First Constructor
    }
    public Employee(int Age)
    {
        // This is the constructor with one parameter.
        // Second Constructor
    }
}

Leave a Reply