Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is constructor overloading in C#?
Constructor overloading means creating multiple constructors in a class with different sets of parameters. This allows creating objects in different ways depending on the data provided.
Click to reveal answer
beginner
Why do we use constructor overloading?
We use constructor overloading to give flexibility when creating objects. It lets us initialize objects with different amounts or types of information easily.
Click to reveal answer
intermediate
How does C# know which constructor to call?
C# chooses the constructor based on the number and types of arguments passed when creating an object. It matches the arguments to the constructor parameters.
Click to reveal answer
intermediate
Can constructors have the same number of parameters but different types?
Yes. Constructors can have the same number of parameters if their types differ. This is called method signature difference and allows overloading.
Click to reveal answer
beginner
Show a simple example of constructor overloading in C#.
class Car {
public string Model;
public int Year;
public Car() {
Model = "Unknown";
Year = 0;
}
public Car(string model) {
Model = model;
Year = 0;
}
public Car(string model, int year) {
Model = model;
Year = year;
}
}
Click to reveal answer
What does constructor overloading allow you to do?
ACreate only one constructor per class
BCreate multiple constructors with different parameters
CChange the class name
DOverride methods in the class
✗ Incorrect
Constructor overloading means having multiple constructors with different parameter lists.
How does C# decide which constructor to use?
ABy the constructor's name
BBy the return type of the constructor
CBy the order constructors are written
DBy matching the number and types of arguments
✗ Incorrect
C# matches the arguments you provide with the constructor parameters to pick the right one.
Can two constructors have the same number of parameters if their types differ?
AYes
BNo
COnly if they have the same names
DOnly if they are static
✗ Incorrect
Constructors can be overloaded if their parameter types differ, even if the count is the same.
What happens if you define no constructor in a C# class?
AThe program will not compile
BThe class cannot be instantiated
CA default parameterless constructor is provided automatically
DYou must define a constructor manually
✗ Incorrect
C# automatically provides a default constructor if none is defined.
Which of these is NOT a benefit of constructor overloading?
AChanging the class inheritance
BAbility to initialize objects differently
CFlexibility in object creation
DCleaner and readable code
✗ Incorrect
Constructor overloading does not affect class inheritance.
Explain constructor overloading and why it is useful in C#.
Think about how you can create objects in different ways.
You got /3 concepts.
Write a simple C# class with at least two overloaded constructors and explain how each works.
Use different parameter lists for each constructor.
You got /4 concepts.
Practice
(1/5)
1. What does constructor overloading in C# allow you to do?
easy
A. Override methods with the same name
B. Create multiple constructors with different parameter lists in the same class
Hint: Check which constructor is called for each object [OK]
Common Mistakes:
Assuming default int value 0 instead of assigned 5
Confusing which constructor runs for each object
Mixing up output order
4. Identify the error in this constructor overloading code:
class Person {
public string name;
public Person(string n) { name = n; }
public Person(string n) { name = n.ToUpper(); }
}
medium
A. Constructor name does not match class name
B. Missing return type in constructors
C. Duplicate constructor with same parameter list
D. Cannot assign string to name
Solution
Step 1: Check constructor parameter lists
Both constructors have the same parameter type and count (string n), causing duplication.
Step 2: Understand overloading rules
Constructors must differ by parameter types or count to overload; identical signatures cause error.
Final Answer:
Duplicate constructor with same parameter list -> Option C
Quick Check:
Same parameters = duplicate constructor error [OK]
Hint: Constructor signatures must differ by parameters [OK]
Common Mistakes:
Thinking constructors can differ by body only
Adding return type mistakenly
Ignoring parameter list uniqueness
5. You want to create a class Rectangle with overloaded constructors: - One constructor takes no parameters and sets width and height to 1. - Another takes one parameter and sets both width and height to that value. - Another takes two parameters to set width and height separately. Which of these constructor definitions correctly implements this?
hard
A. public Rectangle() { width = 1; height = 1; } public Rectangle(int w, int h) { width = w; height = h; } public Rectangle(int w, int h) { width = w; height = h; }
B. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int size) { width = size; height = size; }
C. public Rectangle(int w, int h) { width = w; height = h; } public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; }
D. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; }
Solution
Step 1: Check parameter lists for uniqueness
public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } has three constructors with distinct parameter lists: no parameters, one int, and two ints.
Step 2: Verify each constructor sets values correctly
Each constructor sets width and height as required: default 1, same size, or separate sizes.
Final Answer:
public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } -> Option D
Quick Check:
Distinct parameter lists and correct assignments [OK]
Hint: Each constructor must have unique parameter count or types [OK]
Common Mistakes:
Defining two constructors with same parameter types
Mixing order of constructors causing confusion
Not setting default values in parameterless constructor