Can an async C# method have out parameters?

Posted: (EET/GMT+2)

 

Sometimes, it might be useful to have an asynchronous method which would return out parameters. But the question is, can you do so? The answer: no, you cannot. Unfortunately! If you try to do this, you will get the following compiler error:

Error CS1988: Async methods cannot have ref or out parameters.

This is because of a CLR limitation, as described in this forum post by Microsoft.

While you cannot use out (or ref) parameters in async methods, you can work around this limitation by using custom classes that hold all the information you need to return using out parameters. Alternatively, especially for transient needs, you could use tuples.

Hope this helps!