-1

How can we use async-await in C# with out parameter safely.

for example

async public void someMethod(){
     await someOtherMethod (out string outputFromSomeOtherMethod);
     .......
     .....

} 
KC P
  • 21
  • 1
  • 5
  • 1
    use async void only for event handlers. return a task – Daniel A. White Jan 11 '19 at 02:58
  • In your sample, where method used with `await`, using of `out` parameter is safe enough. Since next line where this parameter can be consumed will be executed only after method completes. – Fabio Jan 11 '19 at 03:28

1 Answers1

7

In short, you don't (and you can't), we use a return

Also, when you think about it, it doesn't make any sense, its a task that finishes when it likes, if you could do that you would be forcing it to wait for the out parameter

public async Task<SomeResult> someOtherMethod() { .. }

...

var myAwesomeResult = await someOtherMethod();

Also, you cold use a delegate, func<T,U> or Action<T> as a parameter

public async Task someOtherMethod(Action<bool> someResult)
{
   await somestuff;
   someResult(true);
}

...

await someOtherMethod(b => YayDoSomethingElse(b));

Ooor as Daniel A. White commented, you could return a ValueTuple if you need easy access to multiple return types

public async Task<(int someValue,string someOtherValue)> someOtherMethod() {.. }
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
TheGeneral
  • 79,002
  • 9
  • 103
  • 141