处理具有收益回报的Enumerable对象的正确模式是什么? [英] What is the proper pattern for handling Enumerable objects with a yield return?

查看:100
本文介绍了处理具有收益回报的Enumerable对象的正确模式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在用于退回Enumerable中所有项目的标准模式?

Does there exist a standard pattern for yield returning all the items within an Enumerable?

比我更喜欢的是,我发现一些代码反映了以下模式:

More often than I like I find some of my code reflecting the following pattern:

    public IEnumerable<object> YieldReturningFunction()
    {

        ...
        [logic and various standard yield return]
        ...

        foreach(object obj in methodReturningEnumerable(x,y,z))
        {
            yield return obj;
        }

    }

foreach循环的显式用法仅用于向我返回可枚举的代码臭味的结果.

The explicit usage of a foreach loop solely to return the results of an Enumerable reeks of code smell to me.

很明显,我可以通过显式地构建一个Enumerable并将其添加到每个标准yield的结果以及添加 methodReturningEnumerable的结果范围的方式来放弃yield return的使用,从而增加代码的复杂性.这很不幸,因为我希望有一种更好的管理收益率模式的方法.

Obviously I could abandon the use of yield return increasing the complexity of my code by explicitly building an Enumerable and adding the result of each standard yield return to it as well as adding a the range of the results of the methodReturningEnumerable. This would be unfortunate, as such I was hoping there exists a better way to manage the yield return pattern.

推荐答案

没有,没有办法解决.

这是一个已被要求的功能,它不是一个坏主意(在其他语言中存在yield foreach或同等功能).

It's a feature that's been requested, and it's not a bad idea (a yield foreach or equivalent exists in other languages).

此时,Microsoft只是没有花时间和金钱来实现它.他们将来可能会执行,也可能不会执行;我会猜测(没有事实依据)它在待办事项列表中;这只是一个问题,如果/何时它在该列表上足够高而不能真正实现.

At this point Microsoft simply hasn't allocated the time and money to implement it. They may or may not implement it in the future; I would guess (with no factual basis) that it's somewhere on the to do list; it's simply a question of if/when it gets high enough on that list to actually get implemented.

我看到的唯一可能的变化是将所有单独的yield returns从方法的顶部重构为它们自己的可枚举的返回方法,然后添加一个新方法来返回该方法的串联,并methodReturningEnumerable(x,y,z).会更好吗?不,可能不是. Concat所添加的内容将与您保存的内容一样多,甚至更多.

The only possible change that I could see would be to refactor out all of the individual yield returns from the top of the method into their own enumerable returning method, and then add a new method that returns the concatenation of that method and methodReturningEnumerable(x,y,z). Would it be better; no, probably not. The Concat would add back in just as much as you would have saved, if not more.

这篇关于处理具有收益回报的Enumerable对象的正确模式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆