Can someone demystify the yield keyword?

I have seen the yield keyword being used quite a lot on Stack Overflow and blogs. I don't use LINQ. Can someone explain the yield keyword? I know that similar questions exist. But none really explain what is its use in plain simple language. By far the best explanation of this (that I've seen) is Jon Skeet's book - and that chapter is free! Chapter 6, C# in Depth. There is no

有人可以揭开yield关键字吗?

我在Stack Overflow和博客中看到yield关键字被大量使用。 我不使用LINQ。 有人可以解释yield关键字吗? 我知道存在类似的问题。 但没有一个真正解释它在纯朴的简单语言中的用途。 迄今为止,对此的最好解释(我见过)是Jon Skeet的书 - 而这一章是免费的! 第6章,C#深入。 这里没有任何我可以添加的内容。 然后购买这本书; 你会成为一个更好的C#程序员。 问:为什么我没有在这里写更长的答案(从评论中转述);

Odd return syntax statement

I know this may sound strange but I don't know even how to search this syntax in internet and also I am not sure what exactly means. So I've watched over some MoreLINQ code and then I noticed this method public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKe

奇数返回语法语句

我知道这可能听起来很奇怪,但我甚至不知道如何在互联网上搜索这种语法,我也不确定究竟是什么意思。 所以我看过一些MoreLINQ代码,然后我注意到了这种方法 public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { if (source == null) throw new ArgumentNullEx

When NOT to use yield (return)

This question already has an answer here: Is there ever a reason to not use 'yield return' when returning an IEnumerable? There are several useful questions here on SO about the benefits of yield return . For example, Can someone demystify the yield keyword Interesting use of the c# yield keyword What is the yield keyword I'm looking for thoughts on when NOT to use yiel

当不使用收益(回报)

这个问题在这里已经有了答案: 返回IEnumerable时,有没有理由不使用“yield return”? 关于yield return的好处,这里有几个有用的问题。 例如, 有人可以揭开yield关键字的神秘面纱吗? 有趣的使用C#收益 关键词 什么是yield关键字 我正在寻找什么时候不使用yield return想法。 例如,如果我期望需要返回一个集合中的所有项目,那么yield似乎不会有用,对吧? 有什么情况下使用yield会受到限制,不必要的,让

Why use the yield keyword, when I could just use an ordinary IEnumerable?

Given this code: IEnumerable<object> FilteredList() { foreach( object item in FullList ) { if( IsItemInPartialList( item ) ) yield return item; } } Why should I not just code it this way?: IEnumerable<object> FilteredList() { var list = new List<object>(); foreach( object item in FullList ) { if( IsItemInPartialList( item )

为什么使用yield关键字时,我只能使用普通的IEnumerable?

鉴于此代码: IEnumerable<object> FilteredList() { foreach( object item in FullList ) { if( IsItemInPartialList( item ) ) yield return item; } } 为什么我不应该这样编码?: IEnumerable<object> FilteredList() { var list = new List<object>(); foreach( object item in FullList ) { if( IsItemInPartialList( item ) ) lis

Proper use of 'yield return'

The yield keyword is one of those keywords in C# that continues to mystify me, and I've never been confident that I'm using it correctly. Of the following two pieces of code, which is the preferred and why? Version 1: Using yield return public static IEnumerable<Product> GetAllProducts() { using (AdventureWorksEntities db = new AdventureWorksEntities()) { var pr

正确使用“收益回报”

yield关键字是C#中的一个关键字,它继续使我神秘化,而且我从未确信我正确使用它。 以下两段代码中,哪些是首选的,为什么? 版本1:使用收益率回报 public static IEnumerable<Product> GetAllProducts() { using (AdventureWorksEntities db = new AdventureWorksEntities()) { var products = from product in db.Product select product; foreach (Product prod

Usefulness of yield

This question already has an answer here: What is the yield keyword used for in C#? 16 answers yield return is a lot more flexible when the data structure is not linear. For example, you could use it to enumerate a tree in preorder, postorder, or inorder: IEnumerable<T> InorderTree<T>(TreeNode<T> node) { if (node.Left != null) { foreach (var x in InorderTree(

收益的有用性

这个问题在这里已经有了答案: 在C#中使用的yield关键字是什么? 16个答案 当数据结构不是线性时, yield return更加灵活。 例如,你可以用它来按顺序,后序或中序枚举一棵树: IEnumerable<T> InorderTree<T>(TreeNode<T> node) { if (node.Left != null) { foreach (var x in InorderTree(node.Left)) { yield return x; } } if (node.Right != null) {

When writing an enumerable, what does yield return var?

Possible Duplicate: What is the yield keyword used for in C#? Say I have code that looks like: (steam is a filestream) using(BinaryWriter bw = new BinaryWriter(stream)) { foreach(byte[] b in BreakBytes(objectOfBytes)) { writer.Write(b); } } So for BreakBytes to work, it has to do something like: public static IEnumerable<byte[]> BreakBytes(byte[] b) { .. while(..) {

在编写一个枚举时,什么产生return var?

可能重复: 在C#中使用的yield关键字是什么? 假设我的代码如下所示: (蒸汽是文件流) using(BinaryWriter bw = new BinaryWriter(stream)) { foreach(byte[] b in BreakBytes(objectOfBytes)) { writer.Write(b); } } 所以对于BreakBytes的工作,它必须做一些事情: public static IEnumerable<byte[]> BreakBytes(byte[] b) { .. while(..) { yield return some_buffer; } .. }

What is the exact usage of Yield Keyword

This question already has an answer here: What is the yield keyword used for in C#? 16 answers Straight from MSDN You use a yield return statement to return each element one at a time.When a yield return statement is reached in the iterator method, expression is returned, and the current location in code is retained. Execution is restarted from that location the next time that the iterato

Yield关键字的确切用法是什么?

这个问题在这里已经有了答案: 在C#中使用的yield关键字是什么? 16个答案 直接从MSDN 您使用yield return语句一次返回一个元素。当在迭代器方法中达到yield return语句时,将返回expression,并保留代码中的当前位置。 下一次调用迭代器函数时,将从该位置重新启动执行。 编辑:要获取详细信息,请参阅此系列文章。 使用yield return来包装已经是IEnumerable的东西是很浪费的。 yield return可用于将不包含IEnum

Trouble understanding yield in C#

This question already has an answer here: What is the yield keyword used for in C#? 16 answers This is called deferred execution, yield is lazy and will only work as much as it needs to. This has great many advantages, one of which being that you can create seemingly infinite enumerations: public IEnumerable<int> InfiniteOnes() { while (true) yield 1; } Now imagine th

在C#中理解产量问题

这个问题在这里已经有了答案: 在C#中使用的yield关键字是什么? 16个答案 这被称为延迟执行, yield是懒惰的,只会按需要工作。 这具有很多优点,其中之一就是您可以创建看似无限的枚举: public IEnumerable<int> InfiniteOnes() { while (true) yield 1; } 现在想象一下: var infiniteOnes = InfiniteOnes(); 会急切地执行,你会非常高兴地发现一个StackOverflow异常。 另一方面,因为懒

Where it is preferred to use yield return in c#?

This question already has an answer here: What is the yield keyword used for in C#? 16 answers is it just because it saves me a row of a list declaration? No. When you use yield you get Deferred Execution. This means that you create the items to yield as the consumer is consuming the items. If you add items to the list and then return it, then you have to create all items before the con

哪里最好在c#中使用yield return?

这个问题在这里已经有了答案: 在C#中使用的yield关键字是什么? 16个答案 是不是因为它为我节省了一行列表声明? 不。当你使用yield你会得到延期执行。 这意味着您创建消费者消费项目时产生的项目。 如果您将项目添加到列表中然后将其返回,那么您必须先创建所有项目,然后消费者才能使用它们中的任何项目。 例如,假设消费者调用您的方法并使用for循环来消耗它,如下所示: foreach(var item in GetMyLovelyItems