I was asked to externally blog my posts to an internal Microsoft email distribution group that I own called CSharp Bits.  This digest is comprised of C# topics that I try to issue daily, typically one topic per post.  It is a tutorial approach so prerequisites are covered.  I have an archive of topics leading up to this one, but I 'm not sure if I will blog those.  I am currently covering how to use reference types in C#.  In this inaugural blog, I pick up at the topic of "Common Methods for All Reference Types"...

 

Common Methods for All Reference Types

The object type provides a number of common methods. Because every type inherits from object, the derived types have these methods too. These common methods include the following:

·                  ToString

·                  Equals

·                  GetType

·                  Finalize

ToString Method

The ToString method returns a string that represents the current object.

The default implementation, as found in the Object class, returns the type name of the class. The following example uses the coordinate example class defined earlier:

coordinate c = new coordinate( );
Console.WriteLine(c.ToString( ));

This example will display "coordinate" on the console.

However, you can override the ToString method in class coordinate to render objects of that type into something more meaningful, such as a string containing the values held in the object.

Equals Method

The Equals method determines whether the specified object is the same instance as the current object. The default implementation of Equals supports reference equality only, as you have already seen.

Subclasses can override this method to support value equality instead.

GetType Method

This method allows extraction of run-time type information from an object. I will discuss this in more detail in the Data Conversions section in a later topic.

Finalize Method

This method is called by the run-time system when an object becomes inaccessible.