1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Rep Power

    Eauality 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 allocation perspective ?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Rep Power
    Regarding memory allocation, do you understand what exactly new does?
    If not, I suggest you read about the function malloc.

    I expect you will find a lot of resource on google explaining the difference and similarity of these two "functions".

    Comparing object is comparing the objects location, unless for Primitive data type.
  4. #3
  5. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Rep Power
    If your type does not overload the Equals() method, you inherit one from the base Object type. The documentation on this method states that, for reference types, it checks for reference equality. This means that it checks whether two variables refer to the same object (the same location in memory).

    Looking at your first sample, your _emp1 and _emp2 variable both refer to the same object. The assignment on the 2nd line did not create a copy, but assigned _emp2 to point to the object created for _emp1. Therefore, when you call the .Equals() method, it sees they are the same object and returns true.

    Looking at your second sample, your _emp1 and _emp2 variables refer to different objects. The second line of code creates a completely new object for _emp2 to refer to. When you call the .Equals() method, it sees that they are different objects and so returns false.

    You can choose to overload the default implementation of .Equals(), and instead look for value equality. Value equality can equate two different objects, if those objects can be said to have the same value. The most common way to do this is to also overload the GetHashCode() method, and simply compare the results of that method for each object inside your .Equals() implementation. In fact, while it's not very clearly documented that way, this is almost required for a class of any substance. It's very easy to make subtle mistakes when overloading this method, but the the current documentation on the GetHashCode() function has a section titled "Notes to Implementors" that is very helpful in avoiding them.
    Last edited by f'lar; January 29th, 2013 at 11:13 PM.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]


IMN logo majestic logo threadwatch logo seochat tools logo