Reference assignment and shared state in C Sharp (C#) - Time & Space Complexity
Let's explore how assigning references affects the time it takes for a program to run.
We want to see how the program's steps grow when we share data between variables.
Analyze the time complexity of the following code snippet.
class Box {
public int Value;
}
void UpdateBox(Box b) {
b.Value = 10;
}
Box box1 = new Box();
Box box2 = box1; // Reference assignment
UpdateBox(box2);
This code creates a box object, assigns its reference to another variable, and updates the value through the second variable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple reference assignment and a single method call.
- How many times: Each operation happens once, no loops or repeated traversals.
Since there are no loops or repeated steps, the number of operations stays the same no matter how big the input is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (assign, assign, update) |
| 100 | 3 (same steps, no change) |
| 1000 | 3 (still the same) |
Pattern observation: The steps do not increase with input size; they stay constant.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input or data is.
[X] Wrong: "Assigning a reference creates a new copy and takes longer as data grows."
[OK] Correct: Assigning a reference just points to the same data; it does not copy or repeat work, so time stays the same.
Understanding how references work helps you explain how programs manage data efficiently without extra cost.
"What if the UpdateBox method contained a loop that changed multiple values inside the Box? How would the time complexity change?"
Practice
ClassA a = new ClassA();
ClassA b = a;Solution
Step 1: Understand reference assignment
Assigning one reference variable to another makes both variables point to the same object in memory.Step 2: Effect of shared reference
Changes made through either variable affect the same object, so both see the changes.Final Answer:
Both variables point to the same object in memory. -> Option AQuick Check:
Reference assignment = shared object [OK]
- Thinking a new object is created on assignment
- Assuming variables become independent
- Confusing value types with reference types
Solution
Step 1: Identify correct assignment operator
In C#, the single equals sign (=) is used for assignment, not double equals (==) or :=.Step 2: Check variable types and initialization
ClassA a = new ClassA(); a = b; correctly declares a reference variable and assigns another reference variable to it.Final Answer:
ClassA a = new ClassA(); a = b; -> Option DQuick Check:
Use = for assignment in C# [OK]
- Using == instead of = for assignment
- Using := which is invalid in C#
- Assigning incompatible types without casting
class Box { public int Value; }
Box box1 = new Box();
box1.Value = 5;
Box box2 = box1;
box2.Value = 10;
Console.WriteLine(box1.Value);Solution
Step 1: Understand reference assignment effect
box2 is assigned to box1, so both point to the same Box object.Step 2: Trace value changes
Changing box2.Value to 10 changes the shared object, so box1.Value is also 10.Final Answer:
10 -> Option CQuick Check:
Shared reference means shared data [OK]
- Assuming box1.Value stays 5 after box2 changes it
- Confusing reference and value types
- Expecting a compile error due to assignment
class Person { public string Name; }
Person p1 = new Person();
p1.Name = "Alice";
Person p2;
p2.Name = "Bob";
p1 = p2;Solution
Step 1: Check variable initialization
p2 is declared but not assigned an object before accessing p2.Name.Step 2: Understand runtime error
Accessing p2.Name causes a NullReferenceException because p2 is null.Final Answer:
p2 is used before being assigned an object. -> Option AQuick Check:
Uninitialized reference causes runtime error [OK]
- Assuming p2 has default object automatically
- Confusing compile-time and runtime errors
- Ignoring null reference exceptions
class Data { public int Number; }
Data d1 = new Data() { Number = 1 };
Data d2 = new Data() { Number = 2 };
d1 = d2;
d2.Number = 5;
Console.WriteLine(d1.Number);Solution
Step 1: Analyze reference assignment
d1 is assigned to d2, so both point to the same object with Number initially 2.Step 2: Trace value change through d2
Changing d2.Number to 5 changes the shared object, so d1.Number is also 5.Final Answer:
5 -> Option BQuick Check:
Reference assignment shares object state [OK]
- Thinking d1.Number remains 1 after assignment
- Confusing value and reference types
- Expecting compile errors on assignment
Ready to prove it?
Pass the test and this pattern moves unknown
