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.

Figure 10.19. object methods that are inherited directly or indirectly by all classes.

(This item is displayed on pages 501 - 502 in the print version)

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:

 
  • It should return false if the argument is null.
  • It should return true if an object is compared to itself, as in object1.Equals( object1 ).
  • It should return true only if both object1.Equals( object2 ) and object2.Equals( object1 ) would return true.
  • For three objects, if object1.Equals( object2 ) returns true and object2.Equals( object3 ) returns TRue, then object1.Equals( object3 ) should also return true.
  • A class that overrides the method Equals should also override the method GetHashCode to ensure that equal objects have identical hashcodes. The default Equals implementation determines only whether two references refer to the same object in memory.

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

Категории