Everything Ultimately Derives from System.Object


Everything Ultimately Derives from System.Object

Given any object, whether a custom class or one built into the system, the methods shown in Table 6.2 will be defined.

Table 6.2. Members of System.Object

Method Name

Description

public virtual bool Equals(object o)

Returns true if the object supplied as a parameter is equal in value, not necessarily in reference, to the instance.

public virtual int GetHashCode()

Returns an integer corresponding to an evenly spread hash code. This is useful for collections such as HashTable collections.

public Type GetType()

Returns an object of type System.Type corresponding to the type of the object instance.

public static bool ReferenceEquals(object a,object b)

Returns true if the two supplied parameters refer to the same object.

public virtual string ToString()

Returns a string representation of the object instance.

public virtual void Finalize()

An alias for the destructor; informs the object to prepare for termination. C# prevents calling this method directly.

protected object MemberwiseClone()

Clones the object in question by performing a shallow copy; references are copied, but not the data within a referenced type.


All of these methods appear on all objects through inheritance; all objects derive (either directly or via an inheritance chain) from object. Even literals include these methods, enabling somewhat peculiar-looking code like this:

Console.WriteLine( 42.ToString() );


Again, everything derives from object , even class definitions that don't have any explicit derivation. The two declarations for PdaItem in Listing 6.21, therefore, result in identical CIL.

Listing 6.21. System.Object Derivation Implied When No Derivation Is Specified Explicitly

 public class PdaItem {   // ... } ___________________________________________________________________________ ___________________________________________________________________________ public class PdaItem : object {   // ... } 

When the object's default implementation isn't sufficient, programmers can override one or more of the three virtual methods. Chapter 9 describes the details for doing this.




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net