1

I'm trying to figure out why is Resharper not showing me a warning about possible multiple enumeration on first method's secondCollection usage, while properly showing it in second method. Can't resharper create some kind of evaluation tree as when iterating with loops? I'm just curious, seems like LINQ is being treaten differently.

private bool LinqMethod(IEnumerable<string> firstCollection, IEnumerable<string> secondCollection)
{
    return !firstCollection.Any(e => secondCollection.Any(c => c != e));
}

private bool LoopMethod(IEnumerable<string> firstCollection, IEnumerable<string> secondCollection)
{
    foreach (var elementA in firstCollection)
        foreach (var elementB in secondCollection)
            if (elementA != elementB)
                return false;

    return true;
}

edit

I've updated my R# to version 9.1.3 - same behaviour. Additionally if I'll inline second Any() call, I'll get this, still not showing the warning

return !firstCollection.Any(e =>
{
    bool any = false;
    foreach (var c in secondCollection)
    {
        if (c != e)
        {
            any = true;
            break;
        }
    }
    return any;
});

However if I'll unroll first Any() call, I'll get the proper warning:

bool any = false;
foreach (var e in firstCollection)
{
    if (secondCollection.Any(c => c != e))
    {
        any = true;
        break;
    }
}
return !any;
5
  • I checked now, showing on my side, :) Aug 17, 2015 at 12:28
  • @X-TECH Is it? What version are you using? I'm not seeing it on VS 2012 + R# 8.1. Maybe it's time for me to update then :)
    – Tarec
    Aug 17, 2015 at 12:30
  • Yes, VS 2013, Resharper 9.0 Aug 17, 2015 at 12:31
  • private bool LoopMethod(IEnumerable<string> firstCollection, IEnumerable<string> secondCollection) { var elementBs = secondCollection as string[] ?? secondCollection.ToArray(); return firstCollection.All(elementA => elementBs.All(elementB => elementA == elementB)); } Aug 17, 2015 at 12:31
  • Thank you, but I'm aware of how to deal with the warning. What I was trying to find out was why wasn't I warned in first scenario. I've updated the answer - I guess it might somehow related with Visual Studio's version.
    – Tarec
    Aug 17, 2015 at 13:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.