gpt4 book ai didi

c# - 在 C# 中,为什么匿名方法不能包含 yield 语句?

转载 作者:IT王子 更新时间:2023-10-29 03:38:13 26 4
gpt4 key购买 nike

我认为做这样的事情会很好(使用 lambda 进行 yield 返回):

public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new()
{
IList<T> list = GetList<T>();
var fun = expression.Compile();

var items = () => {
foreach (var item in list)
if (fun.Invoke(item))
yield return item; // This is not allowed by C#
}

return items.ToList();
}

但是,我发现我不能在匿名方法中使用yield。我想知道为什么。 yield docs就说不允许吧。

由于不允许我创建列表并将项目添加到其中。

最佳答案

Eric Lippert 最近写了一系列关于为什么在某些情况下不允许 yield 的博文。

编辑2:

  • Part 7 (这个是后来发布的,专门解决了这个问题)

你可能会在那里找到答案......


EDIT1:这在 Eric 对 Abhijeet Patel 的评论的回答中的第 5 部分的评论中进行了解释:

问:

Eric,

Can you also provide some insight intowhy "yields" are not allowed inside ananonymous method or lambda expression

一个:

Good question. I would love to haveanonymous iterator blocks. It would betotally awesome to be able to buildyourself a little sequence generatorin-place that closed over localvariables. The reason why not isstraightforward: the benefits don'toutweigh the costs. The awesomeness ofmaking sequence generators in-place isactually pretty small in the grandscheme of things and nominal methodsdo the job well enough in mostscenarios. So the benefits are notthat compelling.

The costs are large. Iteratorrewriting is the most complicatedtransformation in the compiler, andanonymous method rewriting is thesecond most complicated. Anonymousmethods can be inside other anonymousmethods, and anonymous methods can beinside iterator blocks. Therefore,what we do is first we rewrite allanonymous methods so that they becomemethods of a closure class. This isthe second-last thing the compilerdoes before emitting IL for a method.Once that step is done, the iteratorrewriter can assume that there are noanonymous methods in the iteratorblock; they've all be rewrittenalready. Therefore the iteratorrewriter can just concentrate onrewriting the iterator, withoutworrying that there might be anunrealized anonymous method in there.

Also, iterator blocks never "nest",unlike anonymous methods. The iteratorrewriter can assume that all iteratorblocks are "top level".

If anonymous methods are allowed tocontain iterator blocks, then boththose assumptions go out the window.You can have an iterator block thatcontains an anonymous method thatcontains an anonymous method thatcontains an iterator block thatcontains an anonymous method, and...yuck. Now we have to write a rewritingpass that can handle nested iteratorblocks and nested anonymous methods atthe same time, merging our two mostcomplicated algorithms into one farmore complicated algorithm. It wouldbe really hard to design, implement,and test. We are smart enough to doso, I'm sure. We've got a smart teamhere. But we don't want to take onthat large burden for a "nice to havebut not necessary" feature. -- Eric

关于c# - 在 C# 中,为什么匿名方法不能包含 yield 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1217729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com