ReSharper的将删除的foreach产量。为什么? [英] Resharper removes yield from foreach. Why?

查看:157
本文介绍了ReSharper的将删除的foreach产量。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近了解了收益,然后创建了下面的测试控制台程序:

I recently learned about yield and then created the following test console program:

    public static string Customers = "Paul,Fred,Doug,Mark,Josh";
    public static string Admins = "Paul,Doug,Mark";

    public static void Main()
    {
        var test = CreateEfficientObject();

        Console.WriteLine(test.Admins.FirstOrDefault());
        //Note that 'GetAllCustomers' never runs. 
    }

    public static IEnumerable<string> GetAllCustomers()
    {
        var databaseFetch = Customers.Split(',');
        foreach (var s in databaseFetch)
        {
            yield return s;
        }
    }

    public static IEnumerable<string> GetAllAdmins()
    {
        var databaseFetch = Admins.Split(',');
        foreach (var s in databaseFetch)
        {
            yield return s;
        }
    }

    static LoginEntitys CreateEfficientObject()
    {
        var returnObject = new LoginEntitys {};
        returnObject.Admins = GetAllAdmins();
        returnObject.Customers = GetAllCustomers();
        return returnObject;
    }
}
public class LoginEntitys
{
    public IEnumerable<String> Admins { get; set; }
    public IEnumerable<String> Customers { get; set; }
}



然而,我注意到ReSharper的希望将我的foreach 循环为:

public static IEnumerable<string> GetAllCustomers()
{
    var databaseFetch = Customers.Split(',');
    return databaseFetch;
}



为什么ReSharper的想从这种情况下删除的收益呢?它彻底改变了功能,因为它将不再偷懒负荷而不屈服。我只能猜测,要么

Why does Resharper want to remove yield from this case? It changes the functionality completely as it will no longer lazy load without yield. I can only guess that either


  • A)我使用收益错误/中糟糕的初步实践

  • b)这是一个ReSharper的错误/建议,正好可以忽略不计。

  • A) I am using yield incorrectly/in bad pratice
  • B) It's a Resharper bug/suggestion that can just be ignored.

任何有识之士将是巨大的。

Any insight would be great.

推荐答案

您是正确的,这个建议的变革改变了微妙的方式代码的功能,防止它推迟的评价性能及被早评估执行拆分

You are correct that this proposed transformation changes the functionality of the code in subtle ways, preventing it from deferring the evaluation of the properties and performing the Split from being evaluated as early.

或许,那些实现了它非常清楚,它在功能上的变化,并认为它仍然是一个有用的建议,人们如果现有的语义都可能被忽略重要的,或者如果他们实际上没有意识到的语义正在改变。还有我们要知道,我们只能猜测没有什么好办法。如果这些语义对于你的程序很重要,那么你是正确的不使建议的转变。

Perhaps those that implemented it were well aware that it was a change in functionality and felt that it was still a useful suggestion, one that could be ignored if the existing semantics were important, or if they actually failed to realize that the semantics were being altered. There's no good way for us to know, we can only guess. If those semantics are important for your program, then you are correct to not make the suggested transformation.

这篇关于ReSharper的将删除的foreach产量。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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