What is the replacement of Out keyword for Async methods in .Net 4.5 and 4.0?
Asked Answered
S

1

8

All i want to use Out keyword with my Async function. According to MSDN it is not possible Async modifiers not supports to the out keyword. So is there any alternate in .Net framework 4.5/4.0 ?

Soonsooner answered 5/2, 2014 at 7:23 Comment(1)
this could help : msdn.microsoft.com/en-us/library/hh156513.aspxWinded
N
8

You can declare the async function to return Tuple instead. With that the function still able to return multiple values without using out parameter.

public async Task<Tuple<string, int, bool>>SomeFunctionAsync()
{
    return new Tuple<string, int, bool>("foo", 0, false);
}

For Reference :

UPDATE :

you can use shorter syntax as suggested by @svick in comment. Following function return the same value, but using Tuple.Create :

public async Task<Tuple<string, int, bool>>SomeFunctionAsync()
{
    return Tuple.Create("foo", 0, false);
}
Noranorah answered 5/2, 2014 at 8:6 Comment(1)
BTW, Tuple.Create() is often shorter than new Tuple(), because you can use type inference with it.Saideman

© 2022 - 2024 — McMap. All rights reserved.