C# Interview Question #23
What is constructor overloading?
Classes, Constructors & Properties Junior Beginner
Quick Interview Answer
Constructor overloading means defining multiple constructors in the same class with different parameter lists. It allows callers to create objects using different sets of initialization data.
Detailed Explanation
Constructor overloading means defining multiple constructors in the same class with different parameter lists. It allows callers to create objects using different sets of initialization data.
The compiler chooses the appropriate constructor according to the arguments supplied.
Code Example
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public Product() { }
public Product(string name)
{
Name = name;
}
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
}