Ref in async Task
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, you cannot use ref, out, or in parameters on an async method. This is not an arbitrary syntax rule. It exists because async methods are transformed into state machines, and passing by reference into that suspended execution model would create unsafe and confusing lifetime behavior. If you need to return multiple values or mutate shared state, there are better patterns.
Why ref Does Not Work with async
This is invalid:
The compiler rejects it because the async method may suspend and resume later. A by-reference parameter points to storage owned by the caller, and that storage lifetime does not fit cleanly with the generated async state machine.
In short:
- '
asyncmethods can pause and resume' - '
refexpects direct reference semantics to caller-owned storage' - the combination is deliberately disallowed
Return a Value Instead
The simplest replacement is to return the updated value.
Call it like this:
This is usually the cleanest option because the data flow stays explicit.
Return Multiple Values with a Tuple
If the original reason for ref was “I need to update several outputs,” return a tuple.
Usage:
This is a good replacement for out-style patterns in async code.
Use a Mutable Reference Type When Shared State Is Intentional
If you truly need shared mutable state, wrap it in a class and pass the reference type normally.
Usage:
This works because the object reference itself is passed by value, but both caller and callee still see the same object instance.
Be Careful with Shared Mutable State
Just because the mutable-object workaround is possible does not mean it is always a good design. Shared state in async code can create:
- race conditions
- ordering bugs
- unexpected cross-method coupling
If the async method logically computes a result, returning a value is usually better than mutating a shared object.
Use ValueTask or Task the Same Way
The ref restriction is about async, not specifically about Task versus ValueTask. This is still invalid:
Changing the async return type does not make by-reference parameters legal.
Refactoring Old APIs
If you are migrating synchronous code like this:
The async version should usually become:
That keeps the API explicit and async-friendly.
Common Pitfalls
The biggest mistake is trying to fight the compiler and looking for a syntax trick to force ref into an async method. The language intentionally does not support it.
Another issue is replacing ref with a mutable object everywhere, even when returning a value would be much simpler and safer.
Developers also sometimes confuse “reference type” with ref semantics. A class instance can be mutated through its reference, but that is not the same thing as a ref parameter.
Summary
- '
ref,out, andinparameters are not allowed onasyncmethods in C#.' - The restriction exists because async methods are compiled into resumable state machines.
- Return a value or a tuple instead of trying to emulate
refdirectly. - Use a mutable reference type only when shared state is actually the right design.
- Prefer explicit async result flow over hidden mutation whenever possible.
Related reading
- Refactoring a library to be async, how can I avoid repeating myself?
- Refactoring Backgroundworker to async/await
- reference assignment is atomic so why is Interlocked.Exchangeref Object, Object needed?
- Refreshing UITableView Asynchronously after Core Data Loaded Swift
- Reference a .NET Core Library in a .NET 4.6 project
- Reference Microsoft.SqlServer.Smo.dll
- Regarding usage of Task.Start , Task.Run and Task.Factory.StartNew
- Reliably stop System.Threading.Timer?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.