Ref vs Out C#: Which is the Better Choice when?
Tags: csharp, dotnet, tutorial, beginners
Published: 2022-11-29 09:20:04

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](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#passing-an-argument-by-reference)) 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](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#passing-an-argument-by-reference):

```csharp
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:

```csharp
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.