DEV Community

Ravina Deogadkar
Ravina Deogadkar

Posted on

ref and out keyword, Purpose and Use

Hello everyone! In a mid of Coronavirus outbreak, I hope everyone is safe and at home, upgrading themselves. Well, this is my second technical blog, and I would like to discuss two well-known keywords ref and out.

Why ref and out are used?

In C and C++, we have a concept of pass by reference similarly, In C# we can achieve it by using a ref and out keyword. This makes formal parameters an alias to the arguments. They can be used with value type as well as reference type parameters.

Both may look similar but the behavior and purpose are not same. Let's look at each in detail.

ref keyword

ref keyword is used to pass an argument to a function by reference. ref keyword should be used with both method call and method signature, to return a value back to the method caller. If the function caller wants to modify the object's state, the ref return value must be stored in the ref locals.

Alt Text

Here I am assigning a value to the variable which will be passed as ref argument to the method addSum().

Alt Text

out keyword

out keyword passes arguments to function as a reference argument. out keyword must be used with method call and method signature. It is similar to in keyword except that in arguments are not allowed to be modified in the method body.out arguments have to be assigned a value before they are returned.
Assigning a null value to out argument within a method body makes it optional to return value.

Alt Text

Here I am declaring a string variable that will be passed as out argument to the method UpdateMessage(). It is possible to declare a variable in the method calls itself.

Alt Text

Advantages

  • You can return multiple values from a function.
  • The method can be overloaded when one has ref, out and in parameter and the other has value parameter.

Limitations

  • You cannot use ref and out keywords with an async modifier.
  • You cannot overload method which differs only in refs, out and in. It will cause a compile-time error, but no restrictions on overriding.

What differentiates out from ref

  • Variables passed as out arguments do not have to be explicitly initialized before they are passed, unlike ref arguments. But out arguments need to be initialized before they are returned to the calling function.
  • The purpose of out keyword is to return multiple values, on the other hand, the purpose of ref keyword is to pass a value by reference.

How I am using them?

Within my recent project, I am using ref keyword for passing the JSON object as a reference to the function. Passing the JSON object by using reference makes it easy to initialize objects once and then get it filled through the entire life cycle of a class.
While I am using out keyword for conditional initializing an object. A class variable is passed as out argument and then based on conditions appropriate class instance is assigned to class out variable.

It's up to you, how you want to use them.

Happy Coding!

Top comments (0)