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