Diff between Assert.AreEqual and Assert.AreSame?

C#Assert

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPramukaView Question on Stackoverflow
Solution 1 - C#magosView Answer on Stackoverflow
Solution 2 - C#Scott ChamberlainView Answer on Stackoverflow