Passing reference types to methods in C Sharp (C#) - Time & Space Complexity
When we pass reference types to methods, it's important to know how this affects the program's speed.
We want to see how the time to run changes as the input size grows.
Analyze the time complexity of the following code snippet.
void UpdateList(List<int> numbers) {
for (int i = 0; i < numbers.Count; i++) {
numbers[i] = numbers[i] * 2;
}
}
List<int> myNumbers = new List<int> {1, 2, 3, 4, 5};
UpdateList(myNumbers);
This code doubles each number in a list passed to a method.
- Primary operation: Looping through the list to update each element.
- How many times: Once for each item in the list (n times).
As the list gets bigger, the method takes longer because it updates every item.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the list gets bigger.
[X] Wrong: "Passing a reference type to a method makes the method run instantly regardless of list size."
[OK] Correct: Even though the method gets a reference, it still has to loop through all items, so time grows with list size.
Understanding how passing reference types affects time helps you explain code efficiency clearly and confidently.
"What if the method only updated the first 5 items regardless of list size? How would the time complexity change?"
Practice
ref keyword?Solution
Step 1: Understand passing reference types
When a reference type is passed to a method, the method receives a copy of the reference, pointing to the same object.Step 2: Effect on object properties
Modifying the object's properties inside the method changes the original object because both references point to the same memory.Final Answer:
The method can modify the object's properties, affecting the original object. -> Option AQuick Check:
Reference type properties change original [OK]
- Thinking the object is copied entirely
- Confusing reference and value types
- Assuming reassignment changes original reference
Solution
Step 1: Understand parameter passing keywords
Therefkeyword allows the method to reassign the caller's reference variable.Step 2: Analyze each signature
void UpdateObject(ref MyClass obj) { obj = new MyClass(); } usesrefand assigns a new object, so the caller's reference changes. void UpdateObject(MyClass obj) { obj = new MyClass(); } reassigns local copy only. void UpdateObject(out MyClass obj) { obj = new MyClass(); } requires assignment but is for output parameters. void UpdateObject(in MyClass obj) { obj = new MyClass(); } is readonly and cannot reassign.Final Answer:
void UpdateObject(ref MyClass obj) { obj = new MyClass(); } -> Option BQuick Check:
Use ref to reassign reference type parameter [OK]
- Using no keyword and expecting reassignment to affect caller
- Confusing out and ref keywords
- Trying to reassign with in parameter
class Person {
public string Name;
}
void ChangeName(Person p) {
p.Name = "Alice";
p = new Person();
p.Name = "Bob";
}
Person person = new Person();
person.Name = "John";
ChangeName(person);
Console.WriteLine(person.Name);Solution
Step 1: Trace property change before reassignment
The method setsp.Name = "Alice", changing the original object's Name to "Alice".Step 2: Reassignment inside method does not affect caller
Thenpis assigned a new Person object with Name "Bob", but this does not affect the original reference outside the method.Final Answer:
Alice -> Option DQuick Check:
Property change affects original; reassignment does not [OK]
- Thinking reassignment changes original reference
- Expecting output to be 'Bob'
- Ignoring property change before reassignment
class Data {
public int Value;
}
void ResetData(Data d) {
d = new Data();
d.Value = 0;
}
Data data = new Data();
data.Value = 5;
ResetData(data);
Console.WriteLine(data.Value);Solution
Step 1: Understand reassignment effect without ref
Assigningd = new Data()inside the method only changes the local copy of the reference, not the caller's variable.Step 2: Fix by using ref keyword
Usingrefin the method parameter and call allows the method to reassign the caller's reference to the new object.Final Answer:
The method should userefkeyword to reassign the caller's reference. -> Option AQuick Check:
Use ref to reassign reference type parameter [OK]
- Expecting reassignment without ref to affect caller
- Thinking returning new object is required
- Confusing property assignment with reference reassignment
Solution
Step 1: Understand property modification vs reassignment
Modifying properties affects original object without ref, but reassignment does not affect caller unless ref is used.Step 2: Use ref to allow both property changes and reassignment
Passing withreflets method modify properties and replace the object so caller sees all changes.Final Answer:
Pass the object withrefkeyword; modify properties and reassign new object inside method. -> Option CQuick Check:
Use ref to modify and reassign reference type parameter [OK]
- Assuming normal pass allows reassignment to affect caller
- Using out when input object is needed
- Using in which forbids reassignment
Ready to prove it?
Pass the test and this pattern moves unknown
