Comparing two List<string> for equality

Try the following

var equal = expected.SequenceEqual(actual);

Test Version

Assert.IsTrue( actual.SequenceEqual(expected) );

The SequenceEqual extension method will compare the elements of the collection in order for equality.

See http://msdn.microsoft.com/en-us/library/bb348567(v=vs.100).aspx


Many test frameworks offer a CollectionAssert class:

CollectionAssert.AreEqual(expected, actual);

E.g MS Test


You can always write the needed function themselves:

public static bool ListEquals<T>(IList<T> list1, IList<T> list2) {
    if (list1.Count != list2.Count)
        return false;
    for (int i = 0; i < list1.Count; i++)
        if (!list1[i].Equals(list2[i]))
            return false;
    return true;
}

and use it:

// Expected result.
List<string> expected = new List<string>();
expected.Add( "a" );
expected.Add( "b" );
expected.Add( "c" );

// Actual result
actual = new List<string>();
actual.Add( "a" );
actual.Add( "b" );
actual.Add( "c" );

// Verdict
Assert.IsTrue( ListEquals(actual, expected) );