Class object
As we discussed earlier in this chapter, all classes inherit directly or indirectly from the object class (System.Object in the FCL), so its seven methods are inherited by all other classes. Figure 10.19 summarizes object's methods.
Method |
Description |
---|---|
Equals |
This method compares two objects for equality and returns true if they are equal and false otherwise. The method takes any object as an argument. When objects of a particular class must be compared for equality, the class should override method Equals to compare the contents of the two objects. The method's implementation should meet the following requirements: |
|
|
Finalize |
This method cannot be explicitly declared or called. When a class contains a destructor, the compiler implicitly renames it to override the protected method Finalize, which is called only by the garbage collector before it reclaims an object's memory. The garbage collector is not guaranteed to reclaim an object, thus it is not guaranteed that an object's Finalize method will execute. When a derived class's Finalize method executes, it performs its task, then invokes the base class's Finalize method. Finalize's default implementation is a placeholder that simply invokes the base class's Finalize method. |
GetHashCode |
A hashtable is a data structure that relates one object, called the key, to another object, called the value. We discuss Hashtable in Chapter 27, Collections. When initially inserting a value into a hashtable, the key's GetHashCode method is called. The hashcode value returned is used by the hashtable to determine the location at which to insert the corresponding value. The key's hashcode is also used by the hashtable to locate the key's corresponding value. |
GetType |
Every object knows its own type at execution time. Method GetType (used in Section 11.5) returns an object of class Type (namespace System) that contains information about the object's type, such as its class name (obtained from Type property FullName). |
MemberwiseClone |
This protected method, which takes no arguments and returns an object reference, makes a copy of the object on which it is called. The implementation of this method performs a shallow copyinstance variable values in one object are copied into another object of the same type. For reference types, only the references are copied. |
ReferenceEquals |
This static method takes two object arguments and returns TRue if two objects are the same instance or if they are null references. Otherwise, it returns false. |
ToString |
This method (introduced in Section 7.4) returns a string representation of an object. The default implementation of this method returns the namespace followed by a dot and the class name of the object's class. |
We discuss several of object's methods throughout this book (as indicated in the table). You can learn more about object's methods in object's online documentation in the Framework Class Library Reference at:
msdn2.microsoft.com/en-us/library/system.object_members
All array types implicitly inherit from class Array in the System namespace, which in turn extends class object. As a result, like all other objects, an array inherits the members of class object. For more information about the class Array, please see Array's documentation in the FCL Reference, at:
msdn2.microsoft.com/en-us/library/system.array_members