Introduction
The ref and out keywords are likely familiar to everyone who has written C# code, but what do they actually mean? To send data from one function or procedure to another, these two keywords are utilized.
Although they serve similar functions, there are a number of crucial distinctions between the two that you want to be aware of before choosing which one to utilize in your next project.
Let's examine the distinctions between the C# keywords ref and out in more detail and see how you may employ them in your upcoming project.
There are two words that are frequently used interchangeably when dealing with parameters in C#: ref and out. Before utilizing either of these terms in your own code, you should be aware of some significant distinctions even if they both have an identical purpose.
Ref Keyword
When sending or returning references of values to or from methods in C#, the ref keyword is utilized. In essence, it means that since you are altering the value at the address and not simply the value, any change made to a value that is transferred by reference will reflect this change.
It may be used in the following circumstances:
- To reference an argument and pass it to a method.
- To specify a method signature that returns the variable's reference.
- For a struct to be declared as a ref struct
- As a local example
Example
using System;
namespace ref_keyword {
class Demo {
// Main Method
static void Main(string[] args)
{
int p = 10, q = 12;
Console.WriteLine("Initial value of P is {0}", p);
Console.WriteLine("Initial value of Q is {0}", q);
Console.WriteLine();
// Call addValue method by value
addValue(p);
// Display modified value of p
Console.WriteLine("Value of a after addition"+
" operation is {0}", p);
// Call subtractValue method by ref
subtractValue(ref q);
// Display modified value of q
Console.WriteLine("Value of b after "+
"subtraction operation is {0}", q);
}
// Define addValue
// Parameters passed by value
public static void addValue(int p)
{
p += 10;
}
// Define subtractValue
// Parameters passed by ref
public static void subtractValue(ref int q)
{
q -= 5;
}
}
}
Output
Initial value of p is 10
Initial value of q is 12
Value of p after addition operation is 10
Value of q after subtraction operation is 7
Out Keyword
The out keyword in C# allows you to provide arguments of the out-type. It is similar to the reference type, except it does not call for initializing the variable before passing. To pass an argument as an out-type, we must use the out keyword. When we want a function to return several values, it is helpful.
When using our keyword, you need to be aware of the following:
- Both the method declaration and the calling method must explicitly utilize the out keyword to use it as a parameter.
- Asynchronous methods are not permitted to use the out parameters.
- Iterator techniques are not permitted to use the out parameters.
- A method may have more than one out argument.
- Out argument can be declared inline at the moment the method is called. But the same block of code that it calls can also access the inline out parameters.
- It is also possible to overload methods without requiring parameters.
- Since properties are not variables, they cannot be supplied as out arguments.

Join the conversation! Your thoughts help the community grow.