Object instantiation with new in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create new objects in C#, it's helpful to know how this affects the time our program takes to run.
We want to see how the time changes as we make more objects using new.
Analyze the time complexity of the following code snippet.
class Item {}
void CreateItems(int n) {
for (int i = 0; i < n; i++) {
Item item = new Item();
}
}
This code creates n new Item objects one after another in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new
Itemobject inside the loop. - How many times: Exactly
ntimes, once per loop iteration.
Each time we increase n, we create more objects, so the work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 object creations |
| 100 | 100 object creations |
| 1000 | 1000 object creations |
Pattern observation: The number of operations grows in a straight line as n grows.
Time Complexity: O(n)
This means the time to create objects grows directly in proportion to how many objects we make.
[X] Wrong: "Creating objects with new is instant and does not affect time as n grows."
[OK] Correct: Each object creation takes some time, so more objects mean more total time.
Understanding how object creation scales helps you reason about program speed and resource use in real projects.
"What if we created objects inside a nested loop instead of a single loop? How would the time complexity change?"
Practice
new keyword do in C# when used like new MyClass()?Solution
Step 1: Understand the role of
Thenewnewkeyword in C# is used to create a fresh object from a class blueprint.Step 2: Apply to the example
This expression creates a new instance of the classnew MyClass()MyClassby calling its constructor.Final Answer:
It creates a new object instance of the class MyClass. -> Option DQuick Check:
newcreates object = C [OK]
- Thinking new deletes or modifies existing objects
- Confusing new with method calls
- Forgetting parentheses after class name
Person?Solution
Step 1: Check correct use of
In C#, to create a new object, you must usenewkeyword and parenthesesnew ClassName()with parentheses.Step 2: Analyze each option
Person p = new Person(); usesnew Person();correctly. Person p = Person.new(); uses wrong syntax with dot notation. Person p = new Person; misses parentheses. Person p = Person(); missesnew.Final Answer:
Person p = new Person(); -> Option CQuick Check:
Correct syntax = A [OK]
- Omitting parentheses after class name
- Using dot notation with new
- Forgetting the new keyword
class Box {
public int size;
public Box(int s) { size = s; }
}
var b = new Box(5);
Console.WriteLine(b.size);Solution
Step 1: Understand the constructor call
The constructorBox(int s)sets the fieldsizeto the passed values. Here,new Box(5)setssize = 5.Step 2: Check the output of
This prints the value ofConsole.WriteLine(b.size)b.size, which was set to 5 by the constructor.Final Answer:
5 -> Option AQuick Check:
Constructor sets size = 5 [OK]
- Assuming default 0 instead of constructor value
- Confusing null with int fields
- Thinking code won't compile
class Car {
public string model;
public Car(string m) { model = m; }
}
Car c = new Car;Solution
Step 1: Check object instantiation syntax
In C#, when creating a new object, parentheses must follow the class name even if no arguments are passed.Step 2: Analyze the code snippet
The code usesnew Car;without parentheses, which causes a syntax error.Final Answer:
Missing parentheses after Car in object creation. -> Option BQuick Check:
new requires parentheses () [OK]
- Omitting parentheses after new keyword
- Assuming default constructor exists without parentheses
- Ignoring compiler error messages
Student with different names. Which code correctly does this?Solution
Step 1: Understand object creation with parameters
To create objects with different names, call the constructor with the name string for each object separately usingnew Student(name).Step 2: Analyze each option
Student s1 = new Student("Alice"); Student s2 = new Student("Bob"); correctly creates two objects with different names. Student s1 = Student("Alice"); Student s2 = Student("Bob"); missesnew. Student s1, s2 = new Student("Alice"), new Student("Bob"); has invalid syntax for multiple declarations. Student s1 = new Student; Student s2 = new Student; misses parentheses and parameters.Final Answer:
Student s1 = new Student("Alice"); Student s2 = new Student("Bob"); -> Option AQuick Check:
Use new with constructor for each object [OK]
- Forgetting new keyword
- Trying to create multiple objects in one line incorrectly
- Omitting constructor parameters
