The Object Class
The Object class is a special type that is the base class for all other classes and types, including the value types. It defines a set of methods that are therefore inherited by every other type that is defined within the .NET framework class library.
What is the Object Class?
The Object class, held in the System namespace, is the base class for all classes and data types, including the value types. It is the class at the root of the .NET framework class library's entire type hierarchy.
System.Object defines several public and protected methods that, due to inheritance, are automatically made available to all .NET classes, structures and types, including any classes or structures that you create yourself. If you create a class with no base class specified, it will implicitly derive functionality from Object.
Often developers overlookthe Object class. However, its importance is significant and the complexities if its members should be understood.
object = Object
The C# programming language declares a data type named "object". This type is simply an alias for System.Object and so the two terms are interchangeable; they differ in capitalisation but not functionality.
The Object class defines seven base methods. Of these, five are public methods that are available to be called by external objects. The remaining two methods are protected. These are only accessible internally and to derived classes. Each of the methods is described in the following sections.
The Equals method is used to compare two objects to determine if they are equal. The comparison of the objects depends upon their types. For the value types, a bit-by-bit comparison of the two values is made. If they are a perfect match, the method returns true. If not, the method returns false.
When comparing reference types, the values of the two references are compared. Only when both references are pointing to the same object does the method return true. If the properties of two objects are a perfect match but the references are different, the method returns false.
The Equals method can be overridden in a subclass. This permits the behaviour to be changed so that it is more appropriate. For example, in the case of the string data type, Equals is overridden so that a comparison of two strings can be made as though they were value types. Even when the two strings contain different references, if the underlying characters match, the method returns true.
string s1 = "Hello";
string s2 = "Hello";
bool result = s1.Equals(s2); // result = true
The Equals method is available in two forms. The instance version is shown in the above example. In this case, the method requires a single parameter containing the item to be compared to the invoking object. A static version of the method is also available. This requires two parameters, one for each of the items to be compared. The above example could therefore be rewritten as:
string s1 = "Hello";
string s2 = "Hello";
bool result = string.Equals(s1, s2); // result = true
When overriding the behaviour of the Equals method, there are several rules that must be followed to ensure correct operation. These are:
- A call to x.Equals(x), where "x" is a variable of the class in question, must return true. The only exception to this rule is in the comparison of floating point data, where you may decide that a variable containing NaN (not a number) is not equivalent to itself. NB: Interestingly, the floating point types in the .NET framework return true when comparing NaN to NaN using the Equals method, but false when using the == operator. The == operator matches the IEC 60559:1989 specification whilst the Equals method does not.
- A call to x.Equals(y) must return the same result as a call to y.Equals(x).
- The expression "x.Equals(y) && y.Equals(z)" must only return true if x.Equals(z) returns true.
- If x and y are not modified, successive calls to x.Equals(y) must return consistent results.
- A call to x.Equals(null) must return false.
- If the == operator is overloaded, the Equals method must be overridden to provide matching functionality, except in the case of floating point value types.
- If Equals is overridden, the GetHashCode method must also be overridden for compatibility. Otherwise, Hashtables may function incorrectly.
- If a class implements the IComparable interface, the Equals method should be overridden.
- The Equals method must not throw exceptions.
11 May 2008