0
0
CsharpComparisonBeginner · 3 min read

Ref vs Out in C#: Key Differences and When to Use Each

In C#, ref and out both pass arguments by reference, but ref requires the variable to be initialized before passing, while out requires the method to assign a value before returning. Use ref when you want to read and write the argument, and out when the method only outputs a value.
⚖️

Quick Comparison

This table summarizes the main differences between ref and out keywords in C#.

Aspectrefout
Initialization before callMust be initializedNo need to initialize
Assignment inside methodOptionalMust assign before method returns
PurposeRead and write parameterOnly write parameter
Use caseModify existing valueReturn multiple values
Compiler checksChecks variable is initializedChecks variable is definitely assigned
Default value requirementNoNo
⚖️

Key Differences

The ref keyword in C# allows a method to read and modify the value of an argument passed by reference. The variable passed with ref must be initialized before the call, ensuring the method can use its current value. This is useful when you want to update or modify an existing variable.

On the other hand, out is designed for output parameters. The variable passed with out does not need to be initialized before the call, but the method must assign it a value before it returns. This guarantees that the caller receives a value from the method, commonly used to return multiple results.

Both ref and out pass arguments by reference, but their usage intent and compiler rules differ. ref is for input/output parameters, while out is strictly for output parameters.

⚖️

Code Comparison

Here is an example using ref to modify a variable inside a method.

csharp
using System;

class Program
{
    static void AddFive(ref int number)
    {
        number += 5;
    }

    static void Main()
    {
        int value = 10;
        AddFive(ref value);
        Console.WriteLine(value);
    }
}
Output
15
↔️

Out Equivalent

Here is the equivalent example using out to assign a value inside a method.

csharp
using System;

class Program
{
    static void GetFive(out int number)
    {
        number = 5;
    }

    static void Main()
    {
        int value; // Not initialized
        GetFive(out value);
        Console.WriteLine(value);
    }
}
Output
5
🎯

When to Use Which

Choose ref when you want to pass a variable that already has a value and you want the method to read and possibly modify it. This is useful for updating existing data.

Choose out when you want the method to provide a new value back to the caller, especially when returning multiple values from a method. The caller does not need to initialize the variable before the call.

In short, use ref for input/output parameters and out for output-only parameters.

Key Takeaways

ref requires variables to be initialized before passing; out does not.
out parameters must be assigned inside the method before returning.
Use ref to read and modify existing values.
Use out to return new values from methods.
Both pass arguments by reference but serve different purposes.