Question
In C#, I am creating a method where I want to pass an object so the method can modify it. What is the difference between these two method signatures?
public void MyFunction(ref MyClass someClass)
and
public void MyFunction(out MyClass someClass)
Which one should I use, and why?
Short Answer
By the end of this page, you will understand how ref and out work in C#, how they differ, and when each should be used. You will also see how they affect method calls, variable assignment, and code clarity in real programs.
Concept
In C#, method arguments are normally passed by value. That means the method receives its own copy of the variable.
When you use ref or out, you are telling C# to pass the variable by reference instead. That means the method can work with the caller's original variable, not just a copy.
This matters because there are two different needs:
refmeans the variable already has a value before the method is called, and the method may read or change it.outmeans the variable does not need a value before the call, but the method must assign one before returning.
Key difference
ref: input and possibly outputout: output only
Why this matters
Choosing between them communicates intent:
- Use
refwhen the method needs the current value and may update it. - Use
outwhen the method's job is to produce a value for the caller.
This helps other developers understand your code and lets the compiler enforce the right rules.
Important note about objects
MyClass is a reference type, but that does mean and are unnecessary.
Mental Model
Think of a variable as a labeled sticky note.
- With a normal parameter, the method gets a copy of the sticky note.
- With
ref, the method gets access to the original sticky note. It can read what is currently written there and change it. - With
out, the method gets the original sticky note too, but it should treat it like an empty note that must be filled in before leaving.
Another way to think about it:
ref= "Here is a value I already have; you may update it."out= "Please give me a value back through this variable."
Syntax and Examples
Basic syntax
public void UseRef(ref int number)
{
number = number + 1;
}
public void UseOut(out int number)
{
number = 10;
}
The caller must also use the same keyword:
int a = 5;
UseRef(ref a);
int b;
UseOut(out b);
Example with a class
public class MyClass
{
public string Name { get; set; }
}
Using ref
public void ResetPerson()
{
person = MyClass { Name = };
}
MyClass p = MyClass { Name = };
ResetPerson( p);
Console.WriteLine(p.Name);
Step by Step Execution
Consider this example:
public static void BuildMessage(out string message)
{
message = "Hello";
message += " World";
}
string result;
BuildMessage(out result);
Console.WriteLine(result);
Step by step
-
string result;- A variable named
resultis declared. - It does not yet have a value.
- A variable named
-
BuildMessage(out result);- The method receives access to the caller's actual variable.
- Because it is
out, the method must assignmessagebefore returning.
-
Inside the method:
message = "Hello";- The caller's
resultvariable now has the value"Hello".
- The caller's
Real World Use Cases
When ref is used
ref is useful when a method needs an existing value and may update it.
Examples:
- Updating a running total
- Swapping two variables
- Modifying a struct efficiently
- Passing configuration or state that may be changed
Example:
public static void Increment(ref int count)
{
count++;
}
When out is used
out is commonly used when a method tries to produce a result in addition to returning success or failure.
Examples:
- Parsing text into another type
- Returning a computed value from a lookup
- Returning multiple results from one method
Example:
if (int.TryParse("123", out int number))
{
Console.WriteLine(number);
}
This is a classic out pattern:
Real Codebase Usage
In real codebases, ref and out are used less often than beginners expect.
Common pattern: no ref needed for object mutation
Developers often write methods like this:
public void ApplyDiscount(Order order)
{
order.Total = order.Total * 0.9m;
}
Because Order is a reference type, its properties can be updated without ref.
Common pattern: out for Try-methods
A very common convention in .NET is the Try... pattern:
public bool TryGetUser(string id, out User user)
{
user = repository.Find(id);
return user != null;
}
This is used for:
- parsing
- dictionary lookups
- cache retrieval
Common Mistakes
1. Using ref when plain object mutation is enough
Broken thinking:
public void ChangeName(ref MyClass person)
{
person.Name = "Alice";
}
This works, but ref is unnecessary if you are only changing properties.
Better:
public void ChangeName(MyClass person)
{
person.Name = "Alice";
}
2. Forgetting to initialize a ref variable before calling
Broken code:
int x;
UseRef(ref x); // Error
Why it fails:
refrequires the variable to already be assigned.
Correct:
int x = ;
UseRef( x);
Comparisons
| Feature | ref | out | Normal parameter |
|---|---|---|---|
| Passed by reference | Yes | Yes | No |
| Must be assigned before method call | Yes | No | Yes |
| Must be assigned inside method | No | Yes | No |
| Method can read existing value immediately | Yes | No, not safely before assignment | Yes |
| Good for input and output | Yes | No, mainly output | No |
| Good for returning a produced value | Sometimes | Yes |
Cheat Sheet
Quick rules
ref= variable must already have a value before the callout= variable does not need a value before the calloutparameter must be assigned inside the method before it returns- Both
refandoutmust appear in:- the method declaration
- the method call
Syntax
void Update(ref int x)
{
x = x + 1;
}
void Create(out int x)
{
x = 10;
}
Calling
int a = 5;
Update(ref a);
int b;
Create(out b);
Object rule
For classes:
{
obj.Name = ;
}
FAQ
Do I need ref to modify a class object in C#?
No. If you only want to change the object's properties or fields, you can pass the object normally.
When should I use out instead of ref?
Use out when the method is meant to assign and return a value through the parameter, especially in Try... methods.
Can ref and out both change the caller's variable?
Yes. Both pass the variable by reference, so both can change the caller's variable.
Why must an out parameter be assigned inside the method?
Because out means the method is responsible for producing that value. The compiler enforces this rule.
Is out only for primitive types?
No. It can be used with classes, structs, and other types.
If I want to replace an object with a new instance, should I use ref or out?
Use ref if the caller already has a meaningful value and the method may update or replace it. Use if the method is supposed to provide the object from scratch.
Mini Project
Description
Build a small C# console example that demonstrates all three cases: modifying an object's property with a normal parameter, replacing an object with ref, and creating an object with out. This helps you see exactly when ref and out are necessary and when they are not.
Goal
Create a program that shows the difference between normal parameters, ref, and out using a simple Person class.
Requirements
- Create a
Personclass with aNameproperty. - Write one method that changes
Namewithout usingreforout. - Write one method that replaces a
Personobject usingref. - Write one method that creates a new
Personusingout.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Array Initialization Syntaxes Explained
Learn all common C# array initialization syntaxes with examples, rules, comparisons, and mistakes beginners often make.