Constructor Overloading in C sharp

Constructor Overloading


  • Just like we can overload methods in a class we can also overload constructors of class that is a class can be defined with more than one constructor under it.
  • If a class is defined with multiple constructor then instance of the class can be created by using any available constructor. it is not mandatory we need to create the instance by using any particular constructor.
Add a class ‘LoadCon.Cs’ and write the following code


By overloading constructor of a class we can initialized variables of the class in three different ways.
  1. With the help of parameters constructor, we can initialize variable of that class with default values, that is the reason why Parameters constructor we also known as default constructor.
  2. With the help of parameterized constructor we can either initialize all the variables of that class with given values or some variable with given and some variable with default values.
  3. Inheritance based overloading,It is possible to overload a method of parent class under its child and we call this as inheritance based overloading.





No comments:

Post a Comment