11.7 Class object

As we discussed earlier in this chapter, all classes inherit directly or indirectly from class object—an alias for System.Object in the Framework Class Library—so its non-static methods are inherited by all classes. Figure 11.12 summarizes object’s methods. You can learn more about object’s methods at:


http://msdn.microsoft.com/library/system.object

Fig. 11.12 object methods that are inherited directly or indirectly by all classes.

Method Description
Equals This method compares the current object to another object for equality and returns true if they’re equal and false otherwise. It 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 website http://bit.ly/OverridingEqualsCSharp explains the requirements for a properly overridden Equals method.
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’s 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. In general, you should avoid using Finalize.
GetHashCode A hashtable data structure relates objects, called keys, to corresponding objects, called values. We discuss Hashtable in Chapter 21. When a value is initially inserted in a hashtable, the key’s GetHashCode method is called. The 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 12.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’s called. The implementation of this method performs a shallow copy—instance-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 receives two object references and returns true if they’re the same instance or if they’re null references. Otherwise, it returns false.
ToString This method (introduced in Section 7.4) returns a string representation of the current object. The default implementation of this method returns the namespace followed by a dot and the class name of the object’s class.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset