81

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

Pramuka
  • 1,064
  • 1
  • 8
  • 15
  • 1
    Possible duplicate of [What's the difference between Assert.AreNotEqual and Assert.AreNotSame?](http://stackoverflow.com/questions/543263/whats-the-difference-between-assert-arenotequal-and-assert-arenotsame) – Scott Jul 14 '16 at 01:27

2 Answers2

93

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.

magos
  • 3,371
  • 4
  • 29
  • 54
76

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")

DdW
  • 890
  • 1
  • 18
  • 30
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431