0
0
C Sharp (C#)programming~10 mins

Passing reference types to methods in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Passing reference types to methods
Create object in memory
Pass object reference to method
Method receives reference
Modify object properties
Return from method
Original object reflects changes
When you pass a reference type to a method, the method gets a reference to the same object, so changes inside the method affect the original object.
Execution Sample
C Sharp (C#)
class Person {
  public string Name;
}

void ChangeName(Person p) {
  p.Name = "Alice";
}

Person person = new Person();
person.Name = "Bob";
ChangeName(person);
This code creates a Person object with Name 'Bob', passes it to ChangeName, which changes the Name to 'Alice'.
Execution Table
StepActionVariable/ParameterValue BeforeValue AfterNotes
1Create Person objectpersonnullPerson instance with Name=nullObject created in memory
2Assign Nameperson.Namenull"Bob"Set initial name to Bob
3Call ChangeName(person)p (parameter)nullReference to person objectMethod receives reference
4Inside ChangeName: p.Name = "Alice"p.Name"Bob""Alice"Name property changed via reference
5Return from ChangeNameperson.Name"Alice""Alice"Original object reflects change
💡 Method ends; original object's Name is now 'Alice' because reference was passed
Variable Tracker
VariableStartAfter Step 2After Step 4Final
person.Namenull"Bob""Alice""Alice"
p.NamenullN/A"Alice"N/A (parameter scope ends)
Key Moments - 3 Insights
Why does changing p.Name inside the method affect person.Name outside?
Because p is a reference to the same object as person, so modifying p.Name changes the original object's property (see execution_table step 4).
Does the method get a copy of the object or the reference?
The method gets a copy of the reference, not the object itself. Both person and p point to the same object (see execution_table step 3).
What if we assign p = new Person() inside the method?
Assigning p to a new object changes only the local reference p, not the original person variable outside the method.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of p.Name after assignment?
Anull
B"Bob"
C"Alice"
DUndefined
💡 Hint
Check the 'Value After' column for step 4 in execution_table.
At which step does the original person.Name change to "Alice"?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at when p.Name is assigned "Alice" inside the method in execution_table.
If inside ChangeName we assign p = new Person(), what happens to person.Name outside?
Aperson.Name remains unchanged
Bperson.Name changes to new object's Name
Cperson becomes null
Dperson.Name becomes null
💡 Hint
Remember that assigning p to new object changes only local reference, original person stays same.
Concept Snapshot
Passing reference types means methods get a reference to the same object.
Changes to object properties inside the method affect the original object.
The method parameter holds a copy of the reference, not a copy of the object.
Assigning the parameter to a new object does not affect the original reference.
Use this to modify objects inside methods easily.
Full Transcript
In C#, when you pass a reference type like an object to a method, the method receives a copy of the reference pointing to the same object in memory. This means if the method changes properties of the object, those changes are visible outside the method. For example, if you have a Person object with a Name property set to 'Bob' and pass it to a method that changes Name to 'Alice', after the method call, the original Person's Name will be 'Alice'. However, if inside the method you assign the parameter to a new object, this does not affect the original object outside. This behavior is because the reference itself is passed by value, but it points to the same object. Understanding this helps you predict how objects behave when passed to methods.