tags:

views:

644

answers:

2

While trying to verify to myself, that C# Equals for IEnumerables is a reference equals, I found something odd. With the following setup in NUnit

var a = (IEnumerable<string>)(new[] { "one", "two" });
var b = (IEnumerable<string>)(new[] { "one", "two" });

this test

Assert.IsFalse(a.Equals(b));

passes, while this test

Assert.AreNotEqual(a, b);

doesn't. Can anybody explain why?

Edit: Thanks for the answers. I just read the documentation for NUnit, and it says the same thing, that AreEqual and AreNotEqual with collections test for equality of each element of the collection. I guess I was stuck with the notion, that AreEqual and AreNotEqual was just using plain Equals.

+6  A: 

The call to a.Equals(b) returns false because a and b are not the same objects (though they are of course identical enumerations). The Equals method, unless overridden, automatically compares objects by their reference, which is what is happening in this case.

Assert.AreNotEqual is a bit more clever than this. It is designed for debugging purposes, unlike the Equals method, so it in fact compares the sequences yielded by the two enumerations, since it recognises IEnumerable<T> as a special type. You should also notice that it does other interesting things, such as returning true when the two parameters are numerically identical but of different value types (e.g. short and long).

Hope that helps.

Noldorin
+1  A: 

I didn't look at NUnit source code to see how NUnit guys coded AreNotEqual. However, I can tell you how it's done for MbUnit that has the same behavior.

First in AssertNotEqual(a, b) checks if references are equal by executing code like this:

    if (Object.ReferenceEquals(left, right))
        return true;

In your case it's going to fail. Next it checks if the objects are IEnumerable type. If they are, iterate through them and compare if the items are the same and in the same order.

However, in T type in IEnumerable is more complex than string or ValueType in MbUnit AssertNotEqaual(a, b) will not fail.

    var a = (IEnumerable<StringBuilder>)(new[] { new StringBuilder("one"), new StringBuilder("two") });
    var b = (IEnumerable<StringBuilder>)(new[] { new StringBuilder("one"), new StringBuilder("two") });

    Assert.IsFalse(a.Equals(b));  // Success
    Assert.AreNotEqual(a, b);     // Success
Vadim