Diff between Assert.AreEqual and Assert.AreSame?

亡梦爱人 提交于 2019-12-20 10:36:11

问题


What is the difference between Assert.AreEqual and Assert.AreSame?


回答1:


It means that AreSame() checks that they are the exact same object - if reference indicate the same object in memory.

AreEqual() checks that objects has equal type and value. Equal objects can exist in two different places in memory.




回答2:


Assert.AreEqual(a, b) is the same as Assert.IsTrue(Object.Equals(a, b))

Assert.AreSame(a, b) is the same as Assert.IsTrue(Object.ReferenceEquals(a, b))

(the only reason I knew is I just figured it out myself a few hours ago today because I needed to do a Assert.IsTrue(Object.ReferenceEquals(a,b)) and thought "I wonder if there is a better way to do this")



来源:https://stackoverflow.com/questions/24172782/diff-between-assert-areequal-and-assert-aresame

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