C Sharp

The Root of All Types: System.Object

As I've mentioned, all types are ultimately derived from the System.Object type, thereby guaranteeing that every type in the system has a minimum set of abilities. Table 4-1 describes the four public methods that all types get for free.

Table 4-1 Public Methods of the System.Object Type-

Method Name
Description
bool Equals()
This method compares two object references at run time to determine whether they are the exact same object. If the two variables refer to the same object, the return value is true. In the case of value types, which are discussed in the next section, this method returns true if the two types are identical and have the same value.
int GetHashCode()
Retrieves the hash code specified for an object. Hash functions are used when the implementor of a class wants to put an object's hash code in a hash table for performance reasons.
Type GetType()
Used with the reflection methods (discussed in Chapter 16, "Querying Metadata with Reflection") to retrieve the type information for a given object.
string ToString
By default, this method is used to retrieve the name of the object. It can be overridden by derived classes to return a more user-friendly string representation of the object.

Table 4-2 describes the protected methods of System.Object.

Table 4-2 Protected Methods of the System.Object Type-

Method Name
Description
void Finalize()
This method is called by the runtime to allow for cleanup prior to garbage collection. Note that this method may or may not be called.Therefore, do not put code that must run into this method. This rule gets into something called deterministic finalization, which I'll cover in detail in Chapter 5, "Classes."
Object MemberwiseClone
This member represents a shallow copy of the object. By that I mean a copy of an object containing references to other objects that does not include copies of the objects referenced. If you need your class to support a deep copy, which does include copies of the referenced objects, you must implement the ICloneable interface and manually do the cloning, or copying, yourself.

by BrainBellupdated
Advertisement: