Constructor overloading in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use constructor overloading, we have multiple ways to create an object. We want to see how the time to create an object changes as we add more constructors.
Are more constructors making object creation slower?
Analyze the time complexity of the following code snippet.
public class Box
{
public int Width, Height, Depth;
public Box() { Width = Height = Depth = 0; }
public Box(int size) { Width = Height = Depth = size; }
public Box(int width, int height, int depth)
{
Width = width; Height = height; Depth = depth;
}
}
This code shows three constructors for the Box class, each setting dimensions differently.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Assigning values to Width, Height, and Depth fields.
- How many times: Each constructor assigns values exactly three times.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 (one constructor) | 3 assignments |
| 2 (two constructors) | 3 assignments per constructor, but only one runs per object creation |
| 3 (three constructors) | 3 assignments per constructor, still only one runs per object creation |
Pattern observation: Adding more constructors does not increase the work done when creating one object.
Time Complexity: O(1)
This means creating an object takes the same fixed time no matter how many constructors exist.
[X] Wrong: "More constructors mean slower object creation because the program checks all of them."
[OK] Correct: Only one constructor runs when creating an object, so the number of constructors does not slow down creation.
Understanding constructor overloading helps you explain how object creation works efficiently, showing you know how code structure affects performance.
"What if constructors called other constructors inside them? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand constructor overloading concept
Constructor overloading means having more than one constructor in a class, each with a different set of parameters.Step 2: Identify what overloading allows
This allows creating objects in different ways depending on the parameters passed.Final Answer:
Create multiple constructors with different parameter lists in the same class -> Option BQuick Check:
Constructor overloading = multiple constructors with different parameters [OK]
- Thinking only one constructor is allowed
- Confusing overloading with overriding
- Believing constructors must have no parameters
Solution
Step 1: Check constructor syntax
Constructors must have the same name as the class and no return type.Step 2: Identify correct overloading
public class Car { public Car() {} public Car(string model) {} } has two constructors with different parameters and correct syntax.Final Answer:
public class Car { public Car() {} public Car(string model) {} } -> Option AQuick Check:
Constructor syntax correct and overloaded by parameters [OK]
- Adding return type to constructors
- Defining multiple constructors with same parameters
- Omitting access modifier (not mandatory but common style)
class Box {
public int length;
public Box() { length = 5; }
public Box(int l) { length = l; }
}
class Program {
static void Main() {
Box b1 = new Box();
Box b2 = new Box(10);
Console.WriteLine(b1.length + ", " + b2.length);
}
}Solution
Step 1: Analyze constructors called
b1 uses the parameterless constructor setting length = 5; b2 uses the constructor with int parameter setting length = 10.Step 2: Determine printed values
Console.WriteLine prints b1.length (5) and b2.length (10) separated by a comma.Final Answer:
5, 10 -> Option AQuick Check:
Default constructor sets 5, parameterized sets 10 [OK]
- Assuming default int value 0 instead of assigned 5
- Confusing which constructor runs for each object
- Mixing up output order
class Person {
public string name;
public Person(string n) { name = n; }
public Person(string n) { name = n.ToUpper(); }
}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 CQuick Check:
Same parameters = duplicate constructor error [OK]
- Thinking constructors can differ by body only
- Adding return type mistakenly
- Ignoring parameter list uniqueness
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?
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 DQuick Check:
Distinct parameter lists and correct assignments [OK]
- Defining two constructors with same parameter types
- Mixing order of constructors causing confusion
- Not setting default values in parameterless constructor
