开发者

What is the difference (if any) between "yield" and "yield return" in C#?

开发者 https://www.devze.com 2022-12-10 12:01 出处:网络
I\'ve traditionally used yield in C# without the return, e.g.: IEnumerable<T> Foobar() { foreach( var foo in _stuff ) {

I've traditionally used yield in C# without the return, e.g.:

IEnumerable<T> Foobar() {
   foreach( var foo in _stuff ) {
      yield foo;
   }
}

But in other examples I've seen it written as "yield return foo;", see: http://msdn.microsoft.com/en-us/library/9k7k7cf0%28VS.8开发者_高级运维0%29.aspx.

Is there any difference?


C# does not allow yield all by itself - only yield return and yield break.

yield is a contextual keyword that is only recognized inside iterator blocks and only in conjunction with either return or break.

0

精彩评论

暂无评论...
验证码 换一张
取 消