Which of the following code snippets correctly demonstrates constructor overloading in C#?
easy📝 Syntax Q3 of 15
C Sharp (C#) - Classes and Objects
Which of the following code snippets correctly demonstrates constructor overloading in C#?
Aclass Car { public Car() {} public Car(string model) {} }
Bclass Car { public void Car() {} public void Car(string model) {} }
Cclass Car { public Car() {} public Car() {} }
Dclass Car { public Car(int year) {} public Car(int year) {} }
Step-by-Step Solution
Solution:
Step 1: Identify valid constructor syntax
Constructors must have no return type and share the class name.
Step 2: Check for overloading
class Car { public Car() {} public Car(string model) {} } has two constructors with different parameter lists, which is valid overloading.
Step 3: Identify errors in other options
class Car { public void Car() {} public void Car(string model) {} } uses 'void' return type, invalid for constructors. Options C and D have duplicate parameter lists, causing errors.
Final Answer:
class Car { public Car() {} public Car(string model) {} } is the correct constructor overloading syntax.
Quick Check:
Constructor names match class name, no return type, different parameters [OK]
Quick Trick:Constructors have no return type and differ by parameters [OK]
Common Mistakes:
MISTAKES
Adding a return type to constructors
Defining multiple constructors with identical parameters
Using different method names instead of class name
Master "Classes and Objects" in C Sharp (C#)
9 interactive learning modes - each teaches the same concept differently