How to call one constructor from the body of another in C#?
How to call one constructor from the body of another in C#?
155
18-Aug-2023
Updated on 19-Aug-2023
Aryan Kumar
19-Aug-2023To call one constructor from the body of another in C#, you can use the
this()
keyword. Thethis()
keyword refers to the current object.The following code shows how to call one constructor from the body of another:
C#
In this example, the
Person()
constructor is the default constructor. ThePerson(string name)
constructor takes a name parameter. The first line of thePerson(string name)
constructor calls thePerson()
constructor first. This is done to ensure that the fields of thePerson
class are initialized before the name property is set.