Easy differences between ref and out keywords in c#

In this brief comparison, let us discuss what is a ref and out keyword and the key differences between them with an example in C#

One of the properties of value type variables is that they are copied over to the methods or components to which they are passed as parameters.

Sometimes we may need to pass a value type variable as a parameter and still be able to track any variable state changes. Or else you may want to pass multiple values as return type from a method.

For such cases, you can use either a ref or an out keyword.

What is a ref keyword?

The ref keyword is utilized for “call by reference,” where a value type’s reference is handed from the calling method to the method being called. The variable sent as an argument must be initialized beforehand. This method allows both input and output data flow. The method reads the initial value of the variable, can modify it, and the changes reflect outside the method.

It is useful for modifying a variable’s value through a method and enables multiple returns from a method. You need to initialize the variable before passing it to the method, otherwise you will receive a compiler error.

You don’t need to use ref keyword for object types, they are ref types by default.

For the same parameter types, you can use both ref and non-ref variables for method overloading.

What is an out keyword?

In case of an out keyword, you don’t need to initialize the variable before passing it as an argument, but the called method must assign the variable a value before returning control back to the caller.

With an out keyword, data only flows out of the method and the assigned value is available externally. It is used in scenarios where a method needs to return multiple values to the caller.

You must assign a value to the to the out parameters, even if they’re not set before the method call – otherwise you’ll get a compiler error. For the same parameter types, overloaded methods can include both out and non-out versions.

namespace SomeComponent;

public class SomeClassComponent
{
    public void CallerMethod()
    {
        // needs to be assigned else compiler throws error
        int valueToBeReferenced = 10;

        // you don't need to assign a value to the out parameter
        int valueToBeOutParamed;

        CalledMethodWithRef(ref valueToBeReferenced);
        
        // valueToBeReferenced is now modified

        CalledMethodWithOut(out valueToBeOutParamed);
        
        // valueToBeOutParamed now has a value
    }

    void CalledMethodWithRef(ref int value)
    {
        // some modification
        // doesn't throw error
        // since it is assured that
        // value is not unassigned
        value += 10;
    }

    void CalledMethodWithOut(out int value)
    {
        // compulsory assignment
        // else compilation error
        value = 10;
    }
}

5 Differences between ref vs out in C#

ref keywordout keyword
Used when the variable being passed is expected to be modified inside the called method.Used when the called method needs to return more than one return values, since a method can return only one type.
ref variables behave like an in-out parameter where data can be passed into and out of the methodout variables behave like an out parameter where additional data is returned from the called method.
ref variables need to be initialized in the caller method, otherwise you will receive a compiler error.out variable needs to be assigned in the called method before returning to the caller method.
You can use ref keyword in method overloads for the same parameter typeYou can use out keyword in method overloads for the same parameter type
ref keyword is used for value types; object types are reference types by defaultyou can use out keyword for both value types and reference types

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *