1

我认为这是来自 resharper 的误报,但我想征求其他人的意见。也许我错过了一些东西。

我在 Resharper 9.2 和 10 中都发现了这种行为。

考虑这个类:

   public class Foo
   {
      public IEnumerable<string> SomeList { get; set; }
   }

这个方法:

  public void Method(Foo thisFooCanBeNull)
  {
     List<string> theList = thisFooCanBeNull?.SomeList?.ToList();
     if (theList != null && theList.Count > 0) //Possible multiple enumeration of IEnumerable
     {
        foreach (string s in theList) //Possible multiple enumeration of IEnumerable
        {
           //Do something with list.
        }
     }
  }

theList.Countforeach触发警告,但既然我做了一个ToList(),这是不可能的,对吧?

thisFooCanBeNull?.SomeList?.ToList()如果我更改,它不会触发警告thisFooCanBeNull.SomeList?.ToList(),但是由于“这个 foo 可以为空”,所以我不想进行更改。

有人看到这段代码有问题吗?

4

1 回答 1

2

我看不到应该枚举多次的情况。我倾向于相信这是一个与仍然新的 ish 相关的 R# 错误?运算符,特别是因为将其替换为普通的点运算符可以消除错误。

您可能需要通过在 IEnumerable 的任何源中放置断点或调试语句来仔细检查它是否实际上没有多次枚举。

于 2015-11-10T16:34:29.123 回答