ref and out arguments in async method

Does anyone know why async methods are not allowed to have ref and out arguments?

Sure. Think about it - an async method usually returns almost immediately, long before most of the actual logic is executed... that's done asynchronously. So any out parameters would have to be assigned before the first await expression, and there'd quite possibly have to be some restriction on ref parameters to stop them from being used after the first await expression anyway, as after that they may not even be valid.

Consider calling an async method with out and ref parameters, using local variables for the arguments:

int x;
int y = 10;
FooAsync(out x, ref y);

After FooAsync returns, the method itself could return - so those local variables would no longer logically exist... but the async method would still effectively be able to use them in its continuations. Big problems. The compiler could create a new class to capture the variable in the same way that it does for lambda expressions, but that would cause other issues... aside from anything else, you could have a local variable changing at arbitrary points through a method, when continuations run on a different thread. Odd to say the least.

Basically, it doesn't make sense to use out and ref parameters for async methods, due to the timing involved. Use a return type which includes all of the data you're interested in instead.

If you're only interested in the out and ref parameters changing before the first await expression, you can always split the method in two:

public Task<string> FooAsync(out int x, ref int y)
{
    // Assign a value to x here, maybe change y
    return FooAsyncImpl(x, y);
}

private async Task<string> FooAsyncImpl(int x, int y) // Not ref or out!
{
}

EDIT: It would be feasible to have out parameters using Task<T> and assign the value directly within the method just like return values. It would be a bit odd though, and it wouldn't work for ref parameters.


C# is compiled to CIL, and CIL does not support this.

CIL does not have async natively. async methods are compiled to a class, and all (used) parameters and local variables are stored in class fields, so that when a specific method of that class is called, the code knows where to continue executing and what values the variables have.

ref and out parameters are implemented using managed pointers, and class fields of managed pointer type are not allowed, so the compiler cannot preserve the reference passed in.

This restriction on managed pointers in class fields prevents some nonsensical code, as explained in Jon Skeet's answer, as a managed pointer in a class field might refer to a local variable of a function that has already returned. However, this restriction is so strict that even safe and otherwise correct usage is rejected. The ref/out fields could work, if they referred to another class field, and the compiler made sure to always wrap local variables passed with ref/out in a class (like it already knows how to do).

So, C# simply does not have any way to work around the restrictions imposed by CIL. Even if the C# designers want to allow it (I'm not saying they do), they cannot.