Constructors and initialization in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create objects using constructors, some steps run to set up the object.
We want to see how the time needed changes as we create more objects or initialize more data.
Analyze the time complexity of the following code snippet.
public class Box
{
public int[] Items;
public Box(int size)
{
Items = new int[size];
for (int i = 0; i < size; i++)
{
Items[i] = i * 2;
}
}
}
// Creating a Box object
Box myBox = new Box(1000);
This code creates a Box object and fills an array with values during construction.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside the constructor that fills the array.
- How many times: It runs once for each element in the array, so size times.
As the size of the array grows, the number of steps to fill it grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to fill the array |
| 100 | About 100 steps to fill the array |
| 1000 | About 1000 steps to fill the array |
Pattern observation: The work grows directly with the size of the array; doubling the size doubles the work.
Time Complexity: O(n)
This means the time to initialize grows in a straight line with the number of items we set up.
[X] Wrong: "Constructors always run in constant time no matter what."
[OK] Correct: If a constructor does work like filling an array, the time depends on how much data it handles, so it grows with input size.
Understanding how constructors scale helps you explain object setup costs clearly, a useful skill when discussing code efficiency.
"What if the constructor called another method that also loops over the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand what constructors do
Constructors are special methods that run when an object is created to set initial values.Step 2: Compare options with constructor purpose
Only To initialize new objects with starting values describes initializing new objects, which matches the constructor's role.Final Answer:
To initialize new objects with starting values -> Option CQuick Check:
Constructor purpose = initialize objects [OK]
- Confusing constructors with regular methods
- Thinking constructors return values
- Mixing constructors with inheritance
Car?Solution
Step 1: Recall constructor syntax rules
Constructors have the same name as the class and no return type, but must have an access modifier like public.Step 2: Check each option
public Car() { } matches: public + class name + parentheses + no return type. Others have void return or wrong syntax.Final Answer:
public Car() { } -> Option AQuick Check:
Constructor syntax = public ClassName() [OK]
- Adding a return type like void
- Using incorrect parameter syntax
- Omitting access modifier
class Person {
public string Name;
public Person(string name) {
Name = name;
}
}
var p = new Person("Anna");
Console.WriteLine(p.Name);Solution
Step 1: Understand constructor usage
The constructor sets the Name field to the passed string "Anna" when creating the Person object.Step 2: Check output of Console.WriteLine
Since p.Name was set to "Anna", printing p.Name outputs "Anna".Final Answer:
Anna -> Option BQuick Check:
Constructor sets Name = "Anna" so output = Anna [OK]
- Assuming default null value instead of assigned
- Confusing field name with value
- Expecting compilation error due to constructor
class Book {
public string Title;
public Book(string title) {
title = Title;
}
}Solution
Step 1: Analyze the assignment inside constructor
The code assigns title = Title, which sets the parameter to the field's value, not the other way around.Step 2: Correct the assignment direction
It should assign the field Title to the parameter value: Title = title; to initialize properly.Final Answer:
The assignment is reversed; should be Title = title; -> Option DQuick Check:
Field = parameter to initialize correctly [OK]
- Reversing assignment direction
- Changing constructor name incorrectly
- Adding return type to constructor
class Rectangle {
public int Width, Height;
public Rectangle() {
Width = 1;
Height = 1;
}
public Rectangle(int size) {
Width = size;
Height = size;
}
}
var r1 = new Rectangle();
var r2 = new Rectangle(5);
Console.WriteLine(r1.Width + "," + r1.Height);
Console.WriteLine(r2.Width + "," + r2.Height);What is the output?
Solution
Step 1: Understand constructor overloading
The class has two constructors: one with no parameters sets Width and Height to 1; the other sets both to the given size.Step 2: Trace object creation and output
r1 uses the no-parameter constructor, so Width=1, Height=1. r2 uses the int parameter constructor with 5, so Width=5, Height=5.Final Answer:
1,1 5,5 -> Option AQuick Check:
Overloaded constructors set different sizes correctly [OK]
- Assuming default values are zero
- Thinking constructor overload causes error
- Mixing up which constructor runs
