views:

24

answers:

1

How do I test whether two collections are equal as according each pair of elements being equal according to .Equals()?

I find myself writing a little function (given below) which seems over the top. I imagine there must be a far simpler way to do this.

bool ListsEqual<T>(IList<T> lhs, IList<T> rhs) where T : IEquatable<T> {
    if (lhs == rhs) {
        return true;
    }

    if (lhs.Count == rhs.Count) {
        for (int i = 0; i < lhs.Count; i++) {
            if (lhs[i].Equals(rhs[i]) == false) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}
+1  A: 

I found Enumerable.SequenceEqual.

pauldoo