If you’ve ever written code in C#, chances are you have come across the ref and out keywords, but what exactly do they mean? Both of these keywords are used to pass data from one function or method to another.
While their purposes are similar, there are several key differences between the two that you should understand before deciding which one to use in your next project. Let’s take a closer look at the difference between ref and out in C# and see how each keyword might be used in your next project.
What is the difference between Ref and Out C#?
To start analyzing the differences between the two keywords, the ref
keyword is used to pass a variable by reference. This means that the variable can be modified inside of the method.
The out
keyword is also used to pass a variable by reference, but the variable does not have to be initialized before it is passed into the method. Additionally, the out keyword can only be used on variables that are declared inside of the method.
I felt it necessary to make this small nuance or spoiler before going into depth because I wanted to make clear the differences between the two in general terms. Let’s go straight to understanding each one separately.
What is Ref keyword in C#?
Ref
is a keyword that indicates when a variable is an alias of another object or reference. These are the 5 scenarios (according to Microsoft) in which the keyword ref is used:
- Declare a ref struct or maybe a readonly ref struct in a struct declaration.
- To declare that a field is a reference in a ref struct declaration.
- To give an argument to a method via reference in both a method signature and a method call.
- To return a value to a caller via reference in a method signature.
- To specify that a reference return value is kept locally as a reference that the caller wants to alter in a member body. Or even to specify that a local variable refers to another value.
If you are wondering how to use ref, we must use the keyword ref
explicitly for the method and for its (the method’s) definition. Let’s see an example from Microsoft:
void Method(ref int refArgument)
{
refArgument = refArgument + 44;
}
int number = 1;
Method(ref number);
Console.WriteLine(number);
// Output: 45
Although it is true that the arguments of the out parameters do not have to be explicitly initialized before passing them, we must take into account that in the case of passing an argument to a ref parameter, this must be previously initialized.
What is Out keyword in C#?
Out
is a keyword that passes arguments by reference. To understand, all actions performed on the parameter are always performed on the argument.
It is generally used in methods that must return several values, although this parameter does not pass the property.
To use this parameter we must explicitly use the keyword out
when defining the method and also in the called method. Let’s look at the example:
int initializeInMethod;
OutArgExample(out initializeInMethod);
Console.WriteLine(initializeInMethod); // value is now 44
void OutArgExample(out int number)
{
number = 44;
}
The main difference between the two keywords is that ref requires the variable to be initialized before it is passed into the method, while out does not. Additionally, out can only be used on variables declared inside the method.
Main out and ref differences in C#
Now that the function of each keyword is clear, let’s look at the differences between the two:
- With ref the argument must be initialized before passing it to ref. With out it is not necessary to initialize it before.
- With out it is necessary to assign a value to the parameter before returning it to the calling method. With ref this is not necessary.
- With ref the data can be passed in a bi-directional way. With out this is not possible, it can only be done from the called method to the calling method.
- ref is useful when the called method needs to modify the pass parameter as well.
- out is useful when you need to return several values from a method.
Why is one better than the other?
There is no clear advantage of using ref over out, or vice versa. It really depends on the situation and what you are trying to achieve with your code. In general:
- ref is a good choice when you need to pass a variable into a method and you want to be able to modify the variable inside of the method.
- Out is a good choice when you need to pass a variable into a method and you want to initialize the variable inside of the method.
Another thing to consider is that ref parameters must be initialized before they are passed into a method, while out parameters do not have to be initialized. This means that out parameters can be used when the exact value of the parameter is not known before the method is called.
What are the benefits of using Ref?
Although some benefit we have seen a moment ago, let’s do a recap here of the ref
keyword:
- Allows you to pass a variable by reference, which means that the variable can be modified inside of the method.
- Is a good choice when you need to pass a variable into a method and you want to be able to modify the variable inside of the method.
Another benefit of using ref is that it can improve the performance of your code. When you pass a variable by reference, the method can access the variable directly, instead of having to make a copy of the variable. This can save time and memory, especially if the variable is large.
What are the benefits of using Out?
There are also several benefits of using the out
keyword:
- Allows you to pass a variable by reference, but the variable does not have to be initialized before it is passed into the method.
- Can only be used on variables that are declared inside of the method.
- Is a good choice when you need to pass a variable into a method and you want to initialize the variable inside of the method.
Another benefit of using out is that it can help reduce the amount of code you need to write. For example, if you have a method that needs to return two values, you can use out to return both values without having to create a separate method for each value.