How to call one constructor from the body of another in C#?
How to call one constructor from the body of another in C#?
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
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 thePersonclass are initialized before the name property is set.