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. Doubt in Object. Equals() method

Doubt in Object. Equals() method

Scheduled Pinned Locked Moved C#
helpquestionlearning
5 Posts 4 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, I have read in 1 book that the STATIC function Object.Equals() check the object values and Object.ReferenceEquals() check their references. For Ex- Suppose i have a user defined class - Person Person p1 = new Person( 1,"ABC"); Persion p2 = new Person( 1,"ABC"); Object.Equals( p1, p2 ) -> returns true. // compares the state Object.ReferenceEquals( p1,p2) -> returns false.// compares the reference. The output should be like this as specified in that book. But am not getting like this. We cant overide static functions too. Any Idea? It would of great help. Regards

    C C G 3 Replies Last reply
    0
    • D Darmi

      Hi, I have read in 1 book that the STATIC function Object.Equals() check the object values and Object.ReferenceEquals() check their references. For Ex- Suppose i have a user defined class - Person Person p1 = new Person( 1,"ABC"); Persion p2 = new Person( 1,"ABC"); Object.Equals( p1, p2 ) -> returns true. // compares the state Object.ReferenceEquals( p1,p2) -> returns false.// compares the reference. The output should be like this as specified in that book. But am not getting like this. We cant overide static functions too. Any Idea? It would of great help. Regards

      C Offline
      C Offline
      C1AllenS
      wrote on last edited by
      #2

      Hello, The code you have provided is showing correct behavior. Could you please specify what exactly happens at your end. You may refer to the following link in MSDN. http://msdn2.microsoft.com/en-us/library/system.object.referenceequals.aspx[^] I hope this will help. Regards, Allen

      Allen Smith Software Engineer ComponentOne LLC www.componentone.com

      1 Reply Last reply
      0
      • D Darmi

        Hi, I have read in 1 book that the STATIC function Object.Equals() check the object values and Object.ReferenceEquals() check their references. For Ex- Suppose i have a user defined class - Person Person p1 = new Person( 1,"ABC"); Persion p2 = new Person( 1,"ABC"); Object.Equals( p1, p2 ) -> returns true. // compares the state Object.ReferenceEquals( p1,p2) -> returns false.// compares the reference. The output should be like this as specified in that book. But am not getting like this. We cant overide static functions too. Any Idea? It would of great help. Regards

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

        You'll have to override the protected method Equals inherited from Object. This flavor is my own personal favorite, note the use of the private Equals. . I also like overloading the == operator.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(); } } }

        "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

        D 1 Reply Last reply
        0
        • D Darmi

          Hi, I have read in 1 book that the STATIC function Object.Equals() check the object values and Object.ReferenceEquals() check their references. For Ex- Suppose i have a user defined class - Person Person p1 = new Person( 1,"ABC"); Persion p2 = new Person( 1,"ABC"); Object.Equals( p1, p2 ) -> returns true. // compares the state Object.ReferenceEquals( p1,p2) -> returns false.// compares the reference. The output should be like this as specified in that book. But am not getting like this. We cant overide static functions too. Any Idea? It would of great help. Regards

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          The static method Equals(object.object) uses the virtual method Equals(object). You just have to override the Equals(object) method in your Person class.

          Despite everything, the person most likely to be fooling you next is yourself.

          1 Reply Last reply
          0
          • C carbon_golem

            You'll have to override the protected method Equals inherited from Object. This flavor is my own personal favorite, note the use of the private Equals. . I also like overloading the == operator.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(); } } }

            "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

            D Offline
            D Offline
            Darmi
            wrote on last edited by
            #5

            Hi, If we are overriding the protected method Equals(), then whats the difference between the usage of virtual function Equals() and static function Equals defined in the Object class.

            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