Return to Snippet

Revision: 16445
at August 4, 2009 13:11 by pckujawa

Initial Code
public static class Extensions
    static void Main()
		var actual = new List<byte>();
		actual.AddRange(0x01, 0xFF);
		var expected = new byte[] {0x01, 0xFF};
    /// <summary>
    /// Compare objects by their values.
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    public static bool EqualsByValues<T>(this IEnumerable<T> a, IEnumerable<T> b)
        var aEnum = a.GetEnumerator();
        var bEnum = b.GetEnumerator();
        while (aEnum.MoveNext())
            if (!aEnum.Current.Equals(bEnum.Current))
                return false;
        return true;

Initial URL

Initial Description
When I'm running unit tests, I find it very annoying to have to check equality within loops when I've got two collections of data. I know of no built-in way (let me know if there is one) [Edit: [CollectionAssert]( does it. Sometimes you need to turn your IEnumerables into arrays, though, using .ToArray()] to compare two collections' values (such as an array of bytes compared to a list of bytes), so I made this extension method to do it.

Initial Title
Compare iterated types (IEnumerables and their kin) by values (to check for equality)

Initial Tags

Initial Language