Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Confused with the Equals() of Object class [modified]

Confused with the Equals() of Object class [modified]

Scheduled Pinned Locked Moved C#
tutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darmi
    wrote on last edited by
    #1

    Hi, Can anyone please tell me the difference between virtual function Equals(), static function Equals() and Static function ReferenceEquals() defined in Object class. I searched a lot, but didnt get a clear idea about their differences. Am confused with these three. Can anyone give a small example with the output in these 3 cases. Regards

    modified on Wednesday, April 2, 2008 2:10 AM

    N C 2 Replies Last reply
    0
    • D Darmi

      Hi, Can anyone please tell me the difference between virtual function Equals(), static function Equals() and Static function ReferenceEquals() defined in Object class. I searched a lot, but didnt get a clear idea about their differences. Am confused with these three. Can anyone give a small example with the output in these 3 cases. Regards

      modified on Wednesday, April 2, 2008 2:10 AM

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Darmi wrote:

      difference between virtual function Equals(), static function Equals()

      Virtual function Equals(object) checks the current instance with the supplied instance. Consider the following code

      MyClass obj1 = new MyClass();
      MyClass obj2 = new MyClass();
      bool b = obj1.Equals(obj2);

      here obj2 will be compared with obj1. Static Equals(object1,object2) checks the supplied instances (object1 and object2 here).

      MyClass obj1 = new MyClass();
      MyClass obj2 = new MyClass();
      bool b = MyClass.Equals(obj1,obj2);

      Darmi wrote:

      Static function ReferenceEquals()

      This checks the the supplied object instances are having same reference.

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      1 Reply Last reply
      0
      • D Darmi

        Hi, Can anyone please tell me the difference between virtual function Equals(), static function Equals() and Static function ReferenceEquals() defined in Object class. I searched a lot, but didnt get a clear idea about their differences. Am confused with these three. Can anyone give a small example with the output in these 3 cases. Regards

        modified on Wednesday, April 2, 2008 2:10 AM

        C Offline
        C Offline
        carbon_golem
        wrote on last edited by
        #3

        When you call Object.Equals(o1, o2) the static version, internally that function first checks to see if the references are not null, and then calls the virtual method o1.Equals(o2). I just checked that using Reflector so that's accurate. Now here's where it can get tricky, if you do not override the Equals method, when o1.Equals(o2) is called in the static function, it just checks object references. I'm going to repost the code that I previously posted.namespace PersonTest { class Program { static void Main(string[] args) { Person p1 = new Person { Member = "ABC", OtherMember = 1 }; Person p2 = new Person { Member = "ABC", OtherMember = 1 }; Console.WriteLine(Object.Equals(p1, p2).ToString()); Console.WriteLine(Object.ReferenceEquals(p1, p2).ToString()); Console.ReadLine(); } } public class Person { public String Member { get; set; } public Int32 OtherMember { get; set; } public override Boolean Equals(Object obj) { return obj is Person ? Equals(obj as Person) : false; } private Boolean Equals(Person obj) { if (Object.ReferenceEquals(this, obj)) return true; if ((obj == null) || (this == null)) return false; return obj.Member == this.Member && obj.OtherMember == this.OtherMember; } public override int GetHashCode() { return base.GetHashCode(); } } }
        Put this into a project and the output will be True False. Then comment out both of the Equals methods in the Person class and the output will be False False. The reason is that you have 2 different references to Person classes. Regardless of the EQUALITY of the classes the IDENTITY will always be different. Object.ReferenceEquals is always IDENTITY. Object.Equals starts by checking EQUALITY, but then defaults to IDENTITY in the absence of an overridden Equals() method in your class. That's why when you comment out the Equals methods in the Person class, the Object.Equals(p1, p2) the method goes to IDENTITY and not EQUALITY. You have to supply the EQUALITY. I hope this rather thorough explanation helps. Scott

        "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups