yield break in C#

The yield break instruction specifies that the iterator block is to be abandoned prematurely, without having to add more elements. Below is an example of yield break in c#. A return statement in an iterator block is not allowed therefore you must instead use yield break.

static IEnumerable <string> Foo (bool breakit)
  yield return "This One";
  yield return "That One";
  if (breakit)
    yield break;
  yield return "Another One";

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.