Section 5.5. The Root of All Classes: Object


5.5. The Root of All Classes: Object

All C# classes, of any type, are treated as if they ultimately derive from System.Object. Interestingly, this includes value types.

A base class is the immediate "parent" of a derived class. A derived class can be the base to further derived classes, creating an inheritance "tree" or hierarchy. A root class is the topmost class in an inheritance hierarchy. In C#, the root class is Object. The nomenclature is a bit confusing until you imagine an upside-down tree, with the root on top and the derived classes below. Thus, the base class is considered to be "above" the derived class.

C++ programmers take note: C# uses single inheritance with a monolithic class hierarchy: every class inherits from a base class of Object, and multiple inheritance is not possible. However, C# interfaces provide many of the benefits of multiple inheritance. (See Chapter 8 for more information.)


Object provides a number of virtual methods that subclasses can and do override. These include Equals( ) to determine if two objects are the same; GetType(), which returns the type of the object (discussed in Chapter 8); and ToString( ), which returns a string to represent the current object (discussed in Chapter 10). Table 5-1 summarizes the methods of Object.

Table 5-1. The methods of Object

Method

What it does

Equals( )

Evaluates whether two objects are equivalent.

GetHashCode( )

Allows objects to provide their own hash function for use in collections (see Chapter 9).

GetType( )

Provides access to the type object (see Chapter 18).

ToString( )

Provides a string representation of the object.

Finalize( )

Cleans up nonmemory resources; implemented by a destructor (see Chapter 4).

MemberwiseClone ( )

Creates copies of the object; should never be implemented by your type.

ReferenceEquals( )

Evaluates whether two objects refer to the same instance.


Example 5-3 illustrates the use of the ToString( ) method inherited from Object, as well as the fact that primitive datatypes such as int can be treated as if they inherit from Object. Note that the DisplayValue method expects an object, but works perfectly fine if you pass in an integer.

Example 5-3. Inheriting from Object
#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace InheritingFromObject {    public class SomeClass    {       private int val;       public SomeClass( int someVal )       {          val = someVal;       }       public override string ToString( )       {          return val.ToString( );       }    }    public class Tester    {       static void DisplayValue( object o )       {          Console.WriteLine(             "The value of the object passed in is {0}", o.ToString( ) );       }       static void Main( )       {          int i = 5;          Console.WriteLine( "The value of i is: {0}", i.ToString( ) );          DisplayValue( i );          SomeClass s = new SomeClass( 7 );          Console.WriteLine( "The value of s is {0}", s.ToString( ) );          DisplayValue( s );       }    } } Output: The value of i is: 5 The value of the object passed in is 5 The value of s is 7 The value of the object passed in is 7

The documentation for Object.ToString( ) reveals its signature:

public virtual string ToString();

It is a public virtual method that returns a string and that takes no parameters. All the built-in types, such as int, derive from Object and so can invoke Object's methods.

Example 5-3 overrides the virtual function for SomeClass, which is the usual case, so that the class' ToString() method will return a meaningful value. If you comment out the overridden function, the base method will be invoked, which will change the output to:

The value of s is SomeClass

Thus, the default behavior is to return a string with the name of the class itself.

Classes don't need to explicitly declare that they derive from Object; the inheritance is implicit.



Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

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