![]() |
VOOZH | about |
Constructor overloading in C# means writing more than one constructor in the same class, but with different parameters. It lets you create objects in different ways, depending on what information you have when making the object.
We can achieve Constructor overloading in many different ways.
A class may have constructors that accept different numbers of parameters. This allows objects to be initialized with varying levels of detail.
Name: Unknown, Age: 0 Name: John, Age: 18 Name: Alice, Age: 22
Constructors can also differ by the type of parameters. For example, one constructor may accept integers while another accepts doubles.
Result: 30 Result: 10
Even if the types are the same, changing the order of parameters makes constructors distinct.
Name: Alice, Id: 101 Name: Bob, Id: 102