Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - Pass-through for IAsyncEnumerable?

I'd like to know if there's a way I can write a function to "pass through" an IAsyncEnumerable... that is, the function will call another IAsyncEnumerable function and yield all results without having to write a foreach to do it?

I find myself writing this code pattern a lot. Here's an example:

async IAsyncEnumerable<string> MyStringEnumerator();

async IAsyncEnumerable<string> MyFunction()
{
   // ...do some code...

   // Return all elements of the whole stream from the enumerator
   await foreach(var s in MyStringEnumerator())
   {
      yield return s;
   }
}

For whatever reason (due to layered design) my function MyFunction wants to call MyStringEnumerator but then just yield everything without intervention. I have to keep writing these foreach loops to do it. If it were an IEnumerable I would return the IEnumerable. If it were C++ I could write a macro to do it.

What's best practice?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If it were an IEnumerable I would return the IEnumerable.

Well, you can just do the same thing with IAsyncEnumerable (note that the async is removed):

IAsyncEnumerable<string> MyFunction()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 return MyStringEnumerator();
}

However, there's an important semantic consideration here. When calling an enumerator method, the ...do some code... will be executed immediately, and not when the enumerator is enumerated.

// (calling code)
var enumerator = MyFunction(); // `...do some code...` is executed here
...
await foreach (var s in enumerator) // it's not executed here when getting the first `s`
  ...

This is true for both synchronous and asynchronous enumerables.

If you want ...do some code... to be executed when the enumerator is enumerated, then you'll need to use the foreach/yield loop to get the deferred execution semantics:

async IAsyncEnumerable<string> MyFunction()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 await foreach(var s in MyStringEnumerator())
   yield return s;
}

And you would have to use the same pattern in the synchronous world if you wanted deferred execution semantics with a synchronous enumerable, too:

IEnumerable<string> ImmediateExecution()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 return MyStringEnumerator();
}

IEnumerable<string> DeferredExecution()
{
 // ...do some code...

 // Return all elements of the whole stream from the enumerator
 foreach(var s in MyStringEnumerator())
   yield return s;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...