Difference between ref and out parameters

Understand the differences between ref and out parameters in C#, with guidance on when to use each.

The ref Keyword

The ref keyword passes a variable by reference, requiring it to be initialized before being passed, and allows the method to read and modify its value.

The out Keyword

The out keyword also passes a variable by reference, but does not require it to be initialized beforehand, since the called method is expected to assign it a value.

Key Differences

The main difference is that ref requires the variable to already have a value going in, while out does not, and out is typically used when a method needs to return multiple values.

When to Use Each

Use ref when a method needs to both read and update an existing value, and use out when a method's main purpose is to produce one or more new values for the caller.

Conclusion: Both ref and out allow a method to modify the caller's variable, but understanding their initialization requirements helps you choose the right one for each situation.

Side-by-Side Code Examples

Seeing both keywords in action makes the rules easier to remember. Note that the keyword must be written at the call site as well as in the method signature.

// ref: the method reads AND changes an existing value
static void AddBonus(ref int salary, int bonus)
{
    salary += bonus;
}

int pay = 30000;
AddBonus(ref pay, 5000);
Console.WriteLine(pay);          // 35000

// out: the method must produce a value for the caller
static bool TryDivide(int a, int b, out int result)
{
    if (b == 0)
    {
        result = 0;              // still has to assign before returning
        return false;
    }
    result = a / b;
    return true;
}

if (TryDivide(10, 2, out int quotient))
    Console.WriteLine(quotient); // 5

The TryDivide shape is called the Try pattern. The method returns true or false to signal success and hands back the actual value through an out parameter. The framework uses it widely, for example int.TryParse and Dictionary.TryGetValue.

if (int.TryParse("42", out var number))
    Console.WriteLine(number + 1);       // 43

bool isValid = int.TryParse(input, out _);   // discard: only success matters

Rules the Compiler Enforces

Rulerefout
Variable must be assigned before the callYesNo
Method must assign it before returningNoYes, on every path
Method may read it before assigningYesNo
Can declare the variable inline at the callNoYes, out int x
Allowed in async methods and iteratorsNoNo

Two more rules surprise people. You cannot overload two methods that differ only by ref versus out, because they look the same to the runtime. And a property cannot be passed as ref or out, since it is really a pair of methods, not a storage location.

A third keyword, in, also passes by reference but makes the parameter read-only inside the method. It is mainly used to avoid copying large structs.

Reference Types, Tuples and Common Misunderstandings

Beginners often add ref to class parameters to "make changes stick". That is not needed for changing an object's contents, because the method already receives a reference to the same object. You only need ref when the method should replace the caller's variable with a different object.

static void Rename(Person p)      { p.Name = "Asha"; }               // caller sees the change
static void Replace(ref Person p) { p = new Person { Name = "Ravi" }; } // caller's variable now points to a new object

When a method needs to return several values, a tuple is often cleaner than multiple out parameters:

static (int Min, int Max) Range(int[] values) => (values.Min(), values.Max());

var (low, high) = Range(new[] { 4, 9, 1 });   // low = 1, high = 9

Choosing quickly

  • Updating a value the caller already has: ref.
  • Reporting success plus a result: out with the Try pattern.
  • Returning a few related values from normal code: a tuple or a small record.
  • Passing a large struct without copying or modifying it: in.

One more benefit is readability. Because C# makes you write ref or out at the call site, anyone reading AddBonus(ref pay, 5000) knows immediately that pay may change. Use that signal honestly: if a method does not need to modify the caller's variable, pass the value normally and return a result instead. Code with fewer by-reference parameters is easier to test and reason about.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.