对于可能的多个枚举的IEnumerable的处理警告 [英] Handling warning for possible multiple enumeration of IEnumerable

查看:395
本文介绍了对于可能的多个枚举的IEnumerable的处理警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code需要使用的IEnumerable<> 几次从而得到可能的多个枚举的ReSharper的错误的IEnumerable

In my code in need to use an IEnumerable<> several times thus get the Resharper error of "Possible multiple enumeration of IEnumerable".

样品code:

public List<object> Foo(IEnumerable<object> objects)
{
    if (objects == null || !objects.Any())
        throw new ArgumentException();

    var firstObject = objects.First();
    var list = DoSomeThing(firstObject);        
    var secondList = DoSomeThingElse(objects);
    list.AddRange(secondList);

    return list;
}

  • 我可以改变对象参数是列表,然后避免了可能的多个枚举但我不吨得到,我能处理的最高目标。
  • ,我可以做的另一件事是转换的IEnumerable 列表在方法的开头:
    • I can change the objects parameter to be List and then avoid the possible multiple enumeration but then I don't get the highest object that I can handle.
    • Another thing that I can do is to convert the IEnumerable to List at the beginning of the method:
    •  public List<object> Foo(IEnumerable<object> objects)
       {
          var objectList = objects.ToList();
          // ...
       }
      

      但是,这仅仅是尴尬

      你会在这种情况下怎么办?

      What would you do in this scenario?

      推荐答案

      与服用的问题的IEnumerable 作为一个参数是,它告诉来电者:我希望枚举此 。它不会告诉他们你想有多少次枚举。

      The problem with taking IEnumerable as a parameter is that it tells callers "I wish to enumerate this". It doesn't tell them how many times you wish to enumerate.

      我可以改变对象的参数是列表,然后避免了可能的多个枚举但我不明白的我能处理的最高目标的。

      I can change the objects parameter to be List and then avoid the possible multiple enumeration but then I don't get the highest object that I can handle.

      取最高目标的目标是高尚的,但它留下的余地太多的假设。你真的希望有人来传递的LINQ to SQL查询,这种方法,只为你列举了两次(每次获得潜在的不同的结果?)

      The goal of taking the highest object is noble, but it leaves room for too many assumptions. Do you really want someone to pass a LINQ to SQL query to this method, only for you to enumerate it twice (getting potentially different results each time?)

      语义这里缺少的是,来电者,谁可能不会花时间阅读方法的详细信息,可以假设你只重复一次 - 让他们通过你的一个昂贵的对象。你的方法签名并不表示任何一种方式。

      The semantic missing here is that a caller, who perhaps doesn't take time to read the details of the method, may assume you only iterate once - so they pass you an expensive object. Your method signature doesn't indicate either way.

      通过改变方法签名的IList / 的ICollection ,你至少会更清楚地给调用者什么你的期望是,他们可以走冤枉路。

      By changing the method signature to IList/ICollection, you will at least make it clearer to the caller what your expectations are, and they can avoid costly mistakes.

      另外,大部分开发商在寻找方法可能会认为你只是重复一次。如果服用的IEnumerable 是如此的重要,你应该考虑做 .ToList()在方法的开始。

      Otherwise, most developers looking at the method might assume you only iterate once. If taking an IEnumerable is so important, you should consider doing the .ToList() at the start of the method.

      这是一个耻辱。NET不具有一个接口,IEnumerable的+数+索引器,无需添加/删除等方法,这就是我怀疑会解决这个问题。

      It's a shame .NET doesn't have an interface that is IEnumerable + Count + Indexer, without Add/Remove etc. methods, which is what I suspect would solve this problem.

      这篇关于对于可能的多个枚举的IEnumerable的处理警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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