简体   繁体   中英

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

I've traditionally used yield in C# without the return, eg:

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.80%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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM