LetsKickSomeAss in .net LetsKickSomeAss in .net - 11 months ago 44
C# Question

Equality Comparison of two Objects in C#

This is a Fundamental type question, forgive me for being so amateurish.

Case 1:

Employee _emp1 = new Employee();
Employee _emp2 = _emp1;
_emp1.Equals(_emp2) ==> RETURNS a True !!


Employee _emp1 = new Employee();
Employee _emp2 = new Employee();
_emp1.Equals(_emp2) ==> RETURNS a False !!

Could you explain me the above Comparison method and reason interms of Memory-Mapping and allocation perspective ?

Employee _emp1 = new Employee();

Everytime you have new Employee() you have new memory allocated in heap and stack.

means _emp1 is value in memory pointing to heap say 1212.

Now you have second statement

Employee _emp2 = new Employee();

So again new value _emp2 say 1414 in heap.

That's why _emp1.Equals(_emp2) returns false here.

when you say

_emp1 = _emp2

You are assigning the same values in both.