What's the difference between the 'ref' and 'out' keywords?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, both ref and out pass an argument by reference, which means the method works with the caller's variable instead of a copy. The difference is in the contract: ref expects an existing value, while out is for producing a value that the method must assign before returning.
What ref Means
A ref parameter is both input and output. The caller must initialize the variable before the call, and the method can read its current value and replace it.
Because the method reads the incoming value and then updates it, ref is a good fit.
What out Means
An out parameter is output only from the caller's perspective. The caller does not need to initialize the variable first, but the method must assign it before returning.
The method is responsible for producing values, so out communicates that intent clearly.
The Practical Difference
The simplest way to remember the distinction is:
- '
refmeans “this variable already has a meaningful value”' - '
outmeans “this method will fill this variable in”'
That difference affects both readability and compiler rules. The compiler requires initialization before ref, but not before out. The compiler also enforces assignment inside the method for out parameters.
A Common Real-World Example
The classic out pattern is a Try... method such as int.TryParse.
The input is the string. The output is the parsed integer. out fits because the parsed result does not exist before the call.
When ref Is Appropriate
ref is useful when the method truly needs the current value and must update it in place. This is less common in everyday application code than many beginners expect.
The method reads the old value and decides how to mutate it, so ref makes sense.
API Design Considerations
Both keywords are powerful, but they also make method calls less straightforward because the called method can mutate external state. For public APIs, this should be a deliberate choice, not a habit.
In modern C#, alternatives such as tuples or small result objects can sometimes be clearer than multiple out parameters.
This avoids reference parameters entirely when that style is more readable.
Common Pitfalls
The most common mistake is using ref when the method never reads the incoming value. In that case, out usually expresses the contract better.
Another mistake is forgetting that out parameters must be assigned on every control path before the method returns. The compiler enforces this, but it still surprises many developers.
Developers also sometimes overuse ref and out in APIs where returning a tuple or object would make the code easier to understand.
Summary
- Both
refandoutpass arguments by reference. - '
refrequires initialization before the call and supports input plus output.' - '
outdoes not require initialization before the call and is meant for produced values.' - '
TryParse-style methods are classicoutuse cases.' - Use these keywords deliberately because they make mutation across method boundaries explicit.

