Common bugs from reference sharing in C Sharp (C#) - Time & Space Complexity
When objects are shared by reference in C#, some operations repeat depending on how many references exist.
We want to see how the cost grows when multiple parts of code share the same object.
Analyze the time complexity of the following code snippet.
class Box {
public int[] Items;
}
void Modify(Box box) {
for (int i = 0; i < box.Items.Length; i++) {
box.Items[i] += 1;
}
}
Box sharedBox = new Box { Items = new int[1000] };
Modify(sharedBox);
Modify(sharedBox);
This code modifies the same array inside a shared object twice by reference.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array inside the shared object.
- How many times: Twice, because the Modify method is called two times on the same object.
Each call to Modify goes through all items in the array once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 (2 * 10) |
| 100 | 200 (2 * 100) |
| 1000 | 2000 (2 * 1000) |
Pattern observation: The total work doubles because the same array is processed twice.
Time Complexity: O(n)
This means the time grows directly with the size of the array, even if the object is shared and modified multiple times.
[X] Wrong: "Since the object is shared, modifying it multiple times only costs once."
[OK] Correct: Each modification loops through the entire array again, so the cost adds up with each call.
Understanding how shared references affect repeated operations helps you reason about performance and bugs in real code.
"What if the Modify method created a new array instead of changing the shared one? How would the time complexity change?"
Practice
Solution
Step 1: Understand object assignment in C#
Assigning one object variable to another copies the reference, not the object itself.Step 2: Effect of reference copying
Both variables point to the same object in memory, so changes via one affect the other.Final Answer:
Both variables refer to the same object in memory. -> Option AQuick Check:
Reference copy = same object [OK]
- Thinking a new object is created automatically
- Assuming variables hold independent objects
- Confusing value types with reference types
Solution
Step 1: Identify how to copy objects in C#
Assignment copies references, so to create a copy, you need cloning methods like MemberwiseClone() or ICloneable.Step 2: Why other options are incorrect
Assignment operator copies reference; readonly only prevents reassignment; 'new' without copying fields creates a new empty object.Final Answer:
Use a method like MemberwiseClone() or implement ICloneable. -> Option AQuick Check:
Clone method = copy object [OK]
- Using assignment operator expecting a copy
- Confusing readonly with copying
- Creating new object without copying data
class Box { public int Value; }
var box1 = new Box { Value = 5 };
var box2 = box1;
box2.Value = 10;
Console.WriteLine(box1.Value);Solution
Step 1: Understand reference assignment in the code
box2 is assigned box1, so both refer to the same Box object.Step 2: Effect of changing box2.Value
Changing box2.Value to 10 changes the single shared object, so box1.Value also becomes 10.Final Answer:
10 -> Option DQuick Check:
Shared reference means value changes reflect [OK]
- Expecting box1.Value to remain 5
- Confusing value and reference types
- Assuming box2 is a separate object
class Person { public string Name; }
var p1 = new Person { Name = "Alice" };
var p2 = p1;
p2.Name = "Bob";
Console.WriteLine(p1.Name);Solution
Step 1: Analyze object references in the code
p2 is assigned p1, so both variables point to the same Person object.Step 2: Effect of modifying p2.Name
Changing p2.Name to "Bob" changes the shared object, so p1.Name also becomes "Bob".Final Answer:
p2.Name change affects p1.Name because both reference the same object. -> Option BQuick Check:
Shared reference causes shared changes [OK]
- Assuming p1.Name remains "Alice"
- Expecting automatic deep copy on assignment
- Thinking null reference exception occurs
Solution
Step 1: Understand reference sharing with lists
Assigning list2 = list1 copies the reference, so both variables point to the same list.Step 2: Creating an independent copy
Using new List<int>(list1) creates a new list copying elements, so list2 is independent.Final Answer:
var list2 = new List<int>(list1); list2.Add(4); -> Option CQuick Check:
New list constructor copies elements [OK]
- Assigning list2 = list1 expecting copy
- Clearing list1 expecting list2 unaffected
- Reassigning list1 does not affect list2 reference
Ready to prove it?
Pass the test and this pattern moves unknown
