Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diff between Assert.AreEqual and Assert.AreSame?

Tags:

c#

assert

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

like image 407
Pramuka Avatar asked Jun 11 '14 21:06

Pramuka


People also ask

What is assert AreEqual?

Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal.

How do you compare two objects in assert?

Assert. AreEqual() compares references. Usually when comparing lists I compare the count of the items and than some properties of one exact item in the list or directly the item in the list (but again it is the reference).

How do you assert equal in C#?

Assert. Equal(expected.Name, actual.Name); The first example fails due to the way comparison works for reference types. By default, the equality operation for those types will only assert whether the two objects being compared are the same, namely your variables are pointing to the same object within the memory heap.


1 Answers

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.

like image 173
magos Avatar answered Sep 28 '22 02:09

magos