Professional C# 2005 with .NET 3.0

As indicated earlier, all .NET classes are ultimately derived from System.Object. In fact, if you don’t specify a base class when you define a class, the compiler will automatically assume that it derives from Object. Because inheritance has not been used in this chapter, every class you have seen here is actually derived from System.Object. (As noted earlier, for structs this derivation is indirect: A struct is always derived from System.ValueType, which in turn derives from System.Object.)

The practical significance of this is that, besides the methods and properties and so on that you define, you also have access to a number of public and protected member methods that have been defined for the Object class. These methods are available in all other classes that you define.

System.Object Methods

The methods defined in Object are as shown in the following table:

Open table as spreadsheet

Method

Access Modifiers

Purpose

string ToString()

public virtual

Returns a string representation of the object

int GetHashCode()

public virtual

Used if implementing dictionaries (hash tables)

bool Equals(object obj)

public virtual

Compares instances of the object for equality

bool Equals(object objA, object objB)

public static

Compares instances of the object for equality

Type GetType()

Public

Returns details of the type of the object

object MemberwiseClone()

Protected

Makes a shallow copy of the object

void Finalize()

protected virtual

This is the .NET version of a destructor

You haven’t yet seen enough of the C# language to be able to understand how to use all these methods. For the time being, the following list simply summarizes the purpose of each method, with the exception of ToString(), which is examined in more detail.

The ToString() Method

You’ve already encountered ToString() in Chapter 2, “C# Basics.” It provides the most convenient way to get a quick string representation of an object.

For example:

int i = -50; string str = i.ToString(); // returns "-50"

Here’s another example:

enum Colors {Red, Orange, Yellow}; // later on in code... Colors favoriteColor = Colors.Orange; string str = favoriteColor.ToString(); // returns "Orange"

Object.ToString() is actually declared as virtual, and all these examples are taking advantage of the fact that its implementation in the C# predefined data types has been overridden for us in order to return correct string representations of those types. You might not think that the Colors enum counts as a predefined data type. It actually gets implemented as a struct derived from System.Enum, and System.Enum has a rather clever override of ToString() that deals with all the enums you define.

If you don’t override ToString() in classes that you define, your classes will simply inherit the System .Object implementation - which displays the name of the class. If you want ToString() to return a string that contains information about the value of objects of your class, then you will need to override it. To illustrate this, the following example, Money, defines a very simple class, also called Money, which represent U.S. currency amounts. Money simply acts as a wrapper for the decimal class but supplies a ToString() method. Note that this method must be declared as override because it is replacing (overriding) the ToString() method supplied by Object. Chapter 4 discusses overriding in more detail. The complete code for this example is as follows. Note that it also illustrates use of properties to wrap fields:

using System; namespace Wrox.ProCSharp.OOCSharp { class MainEntryPoint { static void Main(string[] args) { Money cash1 = new Money(); cash1.Amount = 40M; Console.WriteLine("cash1.ToString() returns: " + cash1.ToString()); Console.ReadLine(); } } class Money { private decimal amount; public decimal Amount { get { return amount; } set { amount = value; } } public override string ToString() { return "$" + Amount.ToString(); } } }

This example is here just to illustrate syntactical features of C#. C# already has a predefined type to represent currency amounts, decimal, so in real life, you wouldn’t write a class to duplicate this functionality unless you wanted to add various other methods to it. And in many cases, due to formatting requirements, you’d probably use the String.Format() method (which is covered in Chapter 8) rather than ToString() to display a currency string.

In the Main() method, you first instantiate a Money object. The ToString() method is then called, which actually executes the override version of the method. Running this code gives the following results:

StringRepresentations cash1.ToString() returns: $40

Категории