What does “yield break;” do in C#?

后端 未结 8 1952
南方客
南方客 2020-11-28 01:04

I have seen this syntax in MSDN: yield break, but I don\'t know what it does. Does anyone know?

8条回答
  •  独厮守ぢ
    2020-11-28 01:28

    Here http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/ is very good example:

    public static IEnumerable Range( int min, int max )
    {
       while ( true )
       {
          if ( min >= max )
          {
             yield break;
          }
          yield return min++;
       }
    }
    

    and explanation, that if a yield break statement is hit within a method, execution of that method stops with no return. There are some time situations, when you don't want to give any result, then you can use yield break.

提交回复
热议问题