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.
Example
using System;
class Demo{
// Main method
static public void Main()
{
// Declaring variable
// without assigning value
int p;
// Pass variable p to the method
// using out keyword
Addition(out p);
// Display the value p
Console.WriteLine("The addition of the value is: {0}", p);
}
// Method in which out parameter is passed
// and this method returns the value of
// the passed parameter
public static void Addition(out int p)
{
p = 30;
p += p;
}
}
Output
The addition of the value is: 60
Difference Between Ref and Out in C#
Sr. No. |
Key |
Ref keyword |
Out keyword |
1 |
Purpose |
When a called method needs to update the given parameter, the ref keyword is used. |
When a called method needs to update several passed parameters, the out keyword is used. |
2 |
Direction |
The ref keyword is used to pass data back and forth. |
our keyword is used to obtain data in a one-way fashion. |
3 |
Initialization |
A variable must be initialized before being passed as a ref; otherwise, the compiler will throw an error. |
Variables need not be initialized if the out keyword is used. |
4 |
Initialization |
Initializing the argument supplied as a ref in the called method is not necessary. |
The passed-in parameter must be initialized in the called method. |
What one should implement in your project?
Neither utilizing ref over out nor out over ref has a certain advantage. It actually depends on the circumstances and what you want your code to accomplish. When you need to pass a variable into a method and you want to be able to modify the variable inside of the method, ref is generally a smart option. When a variable needs to be passed into a method and the variable needs to be initialized inside the method, out is a decent option.
One other thing to keep in mind is that unlike out parameters, ref parameters need to be initialized before being supplied into a method. This implies that if you want a method to return many values, you can utilize the out parameters.
Whichever one will you use for your project, then? Really, it all depends on what you need. You can always try both and discover which one works better for you if you're unsure.
Summary
When we want to return a value in the same variables that are supplied as arguments, the out and ref keywords come in handy.