I'm using the new Resharper version 6. In several places in my code it has underlined some text and warned me that there may be a Possible multiple enumeration of IEnumerable.
I understand what this means, and have taken the advice where appropriate, but in some cases I'm not sure it's actually a big deal.
Like in the following code:
var properties = Context.ObjectStateManager.GetObjectStateEntry(this).GetModifiedProperties();
if (properties.Contains("Property1") || properties.Contains("Property2") || properties.Contains("Property3")) {
...
}
It's underlining each mention of properties
on the second line, warning that I am enumerating over this IEnumerable multiple times.
If I add .ToList()
to the end of line 1 (turning properties
from a IEnumerable<string>
to a List<string>
), the warnings go away.
But surely, if I convert it to a List, then it will enumerate over the entire IEnumerable to build the List in the first place, and then enumerate over the List as required to find the properties (i.e. 1 full enumeration, and 3 partial enumerations). Whereas in my original code, it is only doing the 3 partial enumerations.
Am I wrong? What is the best method here?
question from:
https://stackoverflow.com/questions/6593825/resharper-possible-multiple-enumeration-of-ienumerable 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…