Understanding Pass by Value and Pass by Reference in C#
When I started C# programming and learning concepts, I found this topic very confusing.
Time passed by and I realized that some topics can also be understood while you are working on an actual project.
Pass-by Value and Pass-by reference are important when working with methods and managing data.
These terms define how arguments are passed to methods and how changes to those arguments affect the original data.
Pass by Value
If there is a method and you pass a parameter to that method by value, a copy of the original data is created.
Any changes made to the parameter inside the method will not affect the original value.
Important points to remember:
Pass by Value is the default behavior in C#.
Value types e.g. , , , Are always passed by value.
What’s happening here?
In , we have taken and assigned it a value.
Then calling .
Inside, a parameter is modified and printed.
The number assignment is optional inside the method. That means if you eliminate ` logic, it would still be fine. But eventually, you will end up with the same value everywhere while printing onto the console.
Output
When the value is modified within the method, the changes are not reflected outside the method.
That’s why, the values passed before and after remain the same, and only the modified value inside the method is changed.
Pass by Reference
In this method, as the name suggests a parameter is passed by reference and a method receives a reference (i.e. a pointer) to the original data.
How it is different from Pass By Value? — If you make any changes to the parameter value inside the method, it will affect the original value.
Important points to remember:
Use the or keyword to pass by reference.
Reference types such as objects, arrays, and strings can be passed by reference.
Changes made to parameters inside the method will reflect outside the method as well.
a) Demonstration By Using "ref”
Explanation
Original value is .
We print its value before modification in .
We modify the value by changing the parameter value to . inside and print its value.
To check if the value has affected the original value, we have printed the original value again.
Output
b) Demonstration By Using “out”
Similar to but the only difference is parameter is expected to be assigned a value inside the method.
Explanation
A variable in is declared not initialized, unlike .
The parameter value is then assigned a new value which is mandatory.
The new value is printed inside .
To check the effect outside this method, we again print the original value in .
Output
What happens if you don’t assign a value to the parameter value inside the ?
Here, I have removed the value assignment inside the . See what I have got.
Using “ref" with Reference Types
We can also use the keyword to reassign a reference to a new array. This means that the ` keyword lets a method change the original array to point to a completely new array.
By using with an array, we can not only modify the contents but change the reference too.
Explanation
We have defined an array and assigned some values to it in .
Printed the first element of that array.
From we pass the reference to an array itself using keyword.
The gets the reference to an array and reassigns it to a new array .
The first element of an array is printed again to check the results.
Output
Here, the contents of the array are modified because the reference type’s contents are accessible inside the method.
When To Use “ref” With Reference Types?
You must use ` with reference types in the following conditions when you want a method
Not only to modify the contents of an object object but also to change the reference to a completely new object.
To avoid making copies of large objects — resulting in performance improvement by reducing memory usage.
To initialize the variable inside the method, which is previously uninitialized.
Real-life example of using "ref"
Explanation:
In , the object is initialized with an old API key and timeout.
We print its value before updating the config value.
The method is then called, passing the object by reference (). This allows the method to replace the entire configuration object with a fresh one (new API key and timeout).
Before the method call, the configuration has the old API key and timeout. After the method call, the configuration is updated with the new values.
Output
Pass by Reference for Reference Types
For reference types, the reference is passed by value by default.
What does it mean? — It means that the method can modify the content of an object but cannot modify or reassign the reference unless it is explicitly passed with or .
Explanation
An integer array is declared and initialized with some values.
We are printing the value of the first element of that array before calling our .
In , the array is passed as an argument, and the first element of an array is changed and assigned a new value.
Finally, we print the value of the first array in that we modified in .
Since arrays in C# are reference types, the method works on the same memory location as the original array. It doesn’t form a copy.
Output
When To Use Pass by Reference for Reference Types?
When you need the method not just just modify the object’s content but also change the reference itself, you can use Pass by Reference for Reference Types.
In other words,
Pass by reference (using or ) is useful when you want a method to either:
Replace the original object completely, rather than just modifying its contents.
Avoid making extra copies of large objects to save memory and improve performance.
Real-life example
Explanation
The list is passed by reference to the method.
Inside the method, the reference of is reassigned to a new list that contains fresh data.
Output
Thank you for reading!
Was This Article Helpful? 📃
If you find this article helpful and if you feel I was able to help you explain basic concepts, please show your appreciation with a like.
Feel free to leave a comment below with your thoughts, experiences, or any additional tips!