Passing reference types to methods lets you change the original object inside the method. This helps when you want to update or modify data without making a copy.
Passing reference types to methods in C Sharp (C#)
void MethodName(ClassName obj) { // use or modify obj }
The method receives a reference to the original object, not a copy.
Changes to the object's properties inside the method affect the original object.
class Person { public string Name; } void ChangeName(Person p) { p.Name = "Alice"; }
void ResetList(List<int> numbers) { numbers.Clear(); }
This program shows how passing a reference type (Person) to a method lets us change its Name property. It also shows clearing a list affects the original list.
using System; using System.Collections.Generic; class Person { public string Name; } class Program { static void ChangeName(Person p) { p.Name = "Alice"; } static void Main() { Person person = new Person(); person.Name = "Bob"; Console.WriteLine("Before: " + person.Name); ChangeName(person); Console.WriteLine("After: " + person.Name); List<int> numbers = new List<int> {1, 2, 3}; Console.WriteLine("List count before: " + numbers.Count); numbers.Clear(); Console.WriteLine("List count after: " + numbers.Count); } }
If you assign a new object to the parameter inside the method, it won't change the original reference outside.
To replace the original object itself, you can use the ref keyword.
Passing reference types means methods get access to the original object.
Modifying the object's properties inside the method changes the original.
Assigning a new object inside the method does not affect the original reference unless ref is used.
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
