It sounds like what you may really be looking for is something like IObservable<T>
, which is sort of like a push-based asynchronous IEnumerable<T>
. See Reactive Extensions, a.k.a. Rx (code licensed under MIT) (no affiliation) for a huge host of methods that work with IObservable<T>
to make it work like LINQ-to-Objects and more.
The problem with IEnumerable<T>
is that there's nothing that really makes the enumeration itself asynchronous. If you don't want to add a dependency on Rx (which is really what makes IObservable<T>
shine), this alternative might work for you:
public async Task<IEnumerable<char>> TestAsync(string testString)
{
return GetChars(testString);
}
private static IEnumerable<char> GetChars(string testString)
{
foreach (char c in testString.ToCharArray())
{
// do other work
yield return c;
}
}
though I'd like to point out that without knowing what's actually being done asynchronously, there may be a much better way to accomplish your goals. None of the code you posted will actually do anything asynchronously, and I don't really know if anything in // do other work
is asynchronous (in which case, this isn't a solution to your underlying problem though it will make your code compile).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…