In this article, we are still going to talk about methods but from a different perspective. We are going to learn how to pass our arguments by using the ref and out keywords.

For the complete navigation of this series check out: C# Back to Basics.

If you want to download the source code for our examples, you can do that from here Ref and Out Keywords Source Code.

This article is divided into several sections:

In the previous post, we were passing a value type arguments while calling our methods. Why is the type of the argument important here?

Well, when we pass the argument of type int, double, decimal etc (basic value types), we do not pass the actual value but its copy. This means that our original values are not changed inside the methods, because we pass a new copy of the original value. As a result, all the operations inside a method are executed upon the copy value.

We can show this in an example:

Value parameter - Ref and Out Keywords in C#

As we can see, the value of the number variable changes only inside the ChangeAndWrite method. But the original value is the same as before calling the ChangeAndWrite method. And again, this is because we are passing the exact copy of the original value.

Using Ref and Out Keywords

We can change this default behavior. If we want to change the original values inside our methods, we can do that by using ref and out keywords inside the method signature and inside the method call as well.

We can use the ref keyword only if the variable which we use as an argument is initialized before calling a method. By using the out keyword, we don’t have to initialize a variable before calling a method but, we must initialize it inside a method.

So, let’s simplify that. If we want to change an existing value of a variable inside a method, we are going to use the ref keyword. But, if we want to assign a completely new value to the variable inside a method, then we use the out keyword.

Example 1 for the Value Type

In the previous example, we saw how the value type variables behave if we don’t use the ref or out keywords. In this one, we are going to see the behavior of value type variables when we use those keywords:

Sending value parameter - Ref and Out Keywords in C#

This all is pretty clear. If we use the ref or the out keyword on the value type variable, its original value will change. But the difference is that with the out keyword we can use unassigned variables.

Example 2 for the Reference Type

We have learned, that the reference type doesn’t store its value inside its own memory location. It stores the address towards the memory location where the value is stored. Therefore when we send an argument as a reference type to the method and change that parameter, the original value is changed. This is because we are not sending the copy of the value but the copy of the address that points to the original value. This is the same thing as when we use the ref keyword with the value types.

Still, we can use the ref keyword with the reference types if we want to create a new object with the same address.

Let’s see all of this in an example:

In the first method, we are not using the ref keyword. The value is changed because we pass the copy of the address in which the original value is stored. In the second method, the original value stays the same. That’s because we create a new object inside method thus the new memory address is allocated. But in the third method, we are using the ref keyword and the original value changes. Why? Because with the ref keyword we are copying the same address to a new object.

Conclusion

Now we know how to use ref and out keywords with the value and reference types. This is quite a useful feature in C#, thus knowing how to work with those keywords is an advantage for the developers.

In the next post, we will talk about recursion and recursive methods.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.