Call By Value and Call by Reference in C#
Understand the difference between call by value and call by reference in C#, and how each affects the way data is passed to methods.
What is Call by Value?
In call by value, a copy of the variable's data is passed to the method. Any changes made to the parameter inside the method have no effect on the original variable back in the calling code.
What is Call by Reference?
In call by reference, using the ref or out keywords, the method receives a reference to the original variable itself, meaning any changes made inside the method directly affect the original value.
Value Types vs Reference Types
It's important to note that even reference types like classes are passed by value by default, meaning the reference itself is copied, though both copies still point to the same underlying object unless ref is explicitly used.
Choosing the Right Approach
Call by value is the safer default for most scenarios since it prevents unintended side effects, while call by reference is useful when a method genuinely needs to modify the caller's original variable or return multiple values.
Tracing What Happens in Memory
By default C# copies the argument into the method's parameter. For a value type like int, the copy is the number itself, so changing the parameter leaves the caller's variable untouched. For a reference type like a class, the copy is the reference — the address — so both the original and the copy point to the same object. That is why you can change an object's fields inside a method and see the effect outside, yet reassigning the parameter to a new object does not change the caller's variable.
void Bump(int n) { n++; } // copy of the value
void Rename(Person p) { p.Name = "Ravi"; } // same object, field changes stick
void Replace(Person p) { p = new Person(); } // only the local copy is reassigned
int x = 5; Bump(x); // x is still 5
var person = new Person{Name="Sita"};
Rename(person); // person.Name is now "Ravi"
Replace(person); // person is unchanged outside
ref vs out and When to Use Each
To let a method change the caller's variable itself, pass it by reference with ref or out.
- ref — the variable must be initialised before the call; the method can read and change it.
- out — the variable need not be initialised first, but the method must assign it before returning. Ideal for returning extra results.
void Swap(ref int a, ref int b) { int t = a; a = b; b = t; }
bool ok = int.TryParse("42", out int value); // out returns the parsed number
Avoiding Surprises With Objects
The most common confusion is expecting ref behaviour from a plain object parameter. Remember the rule: object fields changed inside a method persist because the reference was copied, but reassigning the parameter does not reach the caller unless you used ref. If you only read data, pass by value for safety. Use ref or out deliberately, and sparingly — methods that quietly mutate their arguments are harder to reason about.
Two more keywords round out the picture. The in modifier passes a value type by reference but forbids the method from changing it, which avoids copying a large struct while keeping it read-only. And a method can only use out parameters it is certain to assign on every path, or the code will not compile — the compiler enforces that an out value is always produced before the method returns. Knowing these lets you choose exactly how much access a method gets rather than defaulting to the broadest option.
Takeaway: C# passes copies by default; for value types that protects the original, for reference types the copy still points at the same object. Reach for ref when a method must change the caller's variable and out when it must hand back an extra result.