Thursday, February 14, 2008

Compare IEnumerable (a still better option)

My office mate Chad came up with yet an even better option. You need to be using 3.5 and include the System.Linq extension method namespace. It didn't perform quite as well but pretty close. Even more general purpose. Note that the default SequenceEqual method throws an exception when either operator is null so I handled those cases first.

bool Compare<T>(IEnumerable<T> left, IEnumerable<T> right)


    // handles the same array

    // handles both null

    if (left == right)

        return true;


    // fails when either are null

    if (left == null || right == null)

        return false;


    return left.SequenceEqual(right);



No comments:

Post a Comment