Calling a Constructor from Another Constructor
In this lesson, you will learn how to call a constructor from another constructor.
We have previously learned about this
reference variable. This is the second use case
of the this
reference variable.
We already know that a class can have multiple constructors. There can be a parameterized constructor which initializes all the fields or there can be another constructor which initializes some of the fields and so on. In a case where the code to initialize some of the fields is already written in another constructor, why not just call that other constructor.
In C#, we can call a constructor from another constructor. When you call a constructor from another constructor, you use the this
keyword followed by ()
to refer to the constructor.
Let’s see it in action:
Get hands-on with 1400+ tech skills courses.