In this article I am going to discuss about C# Call by value and Call by reference in detail but before that if you had not read my previous article of C# built-in functions then must read it for more clear understanding. In csharp, this is the very common question that what is the difference between call by value and call by reference so please read this article with more attention.
Introduction to C# Call by Value
In C#, Call by value refers to the method of passing the parameter with any value. i.e Like simply calling the function with parameters. But remember any change inside the method never reflect the actual value outside the function. By using call by value, we ensure that the original value will not change in any case. In simple words when we use call by value then it will create one duplicate of the parameter value inside the memory therefore the actual arguments and passed parameters value of a function refer to different memory locations. Let see an example to understand this more clearly.
C# Program to Explain Call By Value
using System; namespace CallByValueDemo { class Program { static void Increment(int number) { number++; Console.WriteLine("Value Inside Increment method: " + number); } static void Main() { int value = 20; Console.WriteLine("Value Before calling Increment method: " + value); Increment(value); Console.WriteLine("Value After calling Increment method: " + value); } } }
Output:
Value Before calling Increment method: 20
Value Inside Increment method: 21
Value After calling Increment method: 20
Introduction to C# Call by Reference
In C#, Call by reference refers to the method of passing the parameter with value address. Here instead of passing the value we pass the address(reference) of value and if we change the value in the function then it will reflect the change on actual value because here we are working with the address of value not the duplicate of value like in call by value. So remember when we are working with call by reference then change in value always reflect the actual value and the actual arguments and passed parameters of a function refer to the same memory location. Let see an example to understand this more clearly.
C# Program to Explain Call by Reference
using System; namespace CallByReference { class Program { static void ChangeValue(ref int num) { num = 10; Console.WriteLine("Value inside the Method : " + num); } static void Main(string[] args) { int value = 5; Console.WriteLine("Value Before the Method: " + value); ChangeValue(ref value); Console.WriteLine("Value After the Method: " + value); } } }
Output:
Value Before the Method: 5
Value inside the Method : 10
Value After the Method: 10
Difference between C# Call by Value and Call by Reference
We have already discussed about C# Call by value and call by reference with examples and may be you are clear with the difference between C# call by value and call by reference but if you are still not clear with the difference between C# Call by Value and Call by reference then don’t worry you will understand it now with reading this difference between call by value and call by reference.
![csharp call by value and call by reference](http://webtutorialstack.com/wp-content/uploads/2024/03/C-Call-by-Value-and-Call-by-Reference.png)
When to Use Call by Value and Call by Reference?
In programming, there is not any usecase of anything but it will be always a best practice to use the thing at the time when it needs and required. For example if you are writing one program and you does not want to change the actual value but you have to perform the operations based on that value so in that case you can try using call by value to get the copy of actual values to perform the operations without changing the actual value.
Now, if you are writing one program where you have to do certain manipulations or perform number of operations on the value but does not want to create a copies of values. So in that case you can go with call by reference because with call by reference you can change the actual value through methods and it does not create a copy of value.
Conclusion
In this C# tutorials article we had discussed the call by value and call by reference in csharp with examples and also understand when to use call by value and call by reference. Hope you found this article helpful and like this article. In my next article I am going to discuss about the recursion in detail.