Object Identity vs. Object Equality - Overriding System.Object.Equals(Object obj)

I have started a new technical blog here-

http://codebetter.com/blogs/david.hayden/

This blog will be used for .NET Technical Articles and Tutorials (no announcements and rarely, if any, personal stuff as usual).  I will mention the articles here as much as possible.

One article, Object Identity vs. Object Equality - Overriding System.Object.Equals(Object obj), is up on the blog already as well as a few others.  Here is a quick description:

“By default, your class inherits from System.Object.  System.Object has a virtual method Equals that your instances will inherit if you choose not to override the method.

System.Object takes a simple view of object equality and just tests if two objects are the same instance (e.g. share the same address in memory), which is often referred to as Object Identity. If the objects being compared are the same instance, they are considered equal.  If they are the not the same instance, they are considered not equal.

If the above test for equality is fine for your class, I recommend the simplest course of action - do not override the method.

However, many times the test for equality between two objects is not about referential semantics, but value semantics.  In other words, equality may mean more about the objects having the same field values and not that they are the same instance.”  Read more.

posted on Sunday, February 27, 2005 3:21 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices