C# Assert.AreNotEqual versus Equals

醉酒当歌 提交于 2019-12-10 15:37:41

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/789818/c-sharp-assert-arenotequal-versus-equals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!