Confused with the Equals() of Object class [modified]
-
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
-
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
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 codeMyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
bool b = obj1.Equals(obj2);here
obj2
will be compared withobj1
. StaticEquals(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
-
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
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