System.Object


Since all classes inherit from System.Object, all classes will have access to the protected and public members of this class. This means that it is well worth taking a look at what is available there. System.Object contains the methods shown in the following table.

Method

Return Type

Virtual

Static

Description

Object()

N/A

No

No

Constructor for the System.Object type. Automatically called by constructors of derived types.

~Object() (also known as Finalize() — see next section)

N/A

No

No

Destructor for the System.Object type. Automatically called by destructors of derived types, cannot be called manually.

Equals(object)

bool

Yes

No

Compares the object for which this method is called with another object and returns true if they are equal. The default implementation checks to see if the object parameter refers to the same object (because objects are reference types). This method can be overridden if you wish to compare objects in a different way, for example if they hold equivalent data.

Equals(object, object)

bool

No

Yes

This method compares the two objects passed to it and checks to see if they are equal This check is performed using the Equals(object) method. Note that if both objects are null references this method returns true.

ReferenceEquals (object, object)

bool

No

Yes

This method compares the two objects passed to it and checks to see if they are references to the same instance.

ToString()

string

Yes

No

Returns a string corresponding to the object instance. By default, this is the qualified name of the class type (see earlier example), but this can be overridden to provide an implementation appropriate to the class type.

MemberwiseClone()

object

No

No

Copies the object by creating a new object instance and copying members. Note that this member copying will not result in new instances of these members. Any reference type members of the new object will refer to the same objects as the original class. This method is protected and so can only be used from within the class or from derived classes.

GetType()

System.Type

No

No

Returns the type of the object in the form of a System.Type object.

GetHashCode()

int

Yes

No

Used as a hash function for objects where this is required. A hash function is one that returns a value identifying the object state in some compressed form.

These methods are the basic ones that must be supported by object types in the .NET Framework, although you might never use some of them (or use them only in special circumstances, such as GetHashCode()).

GetType() is a useful method when you are using polymorphism, because it allows you to perform different operations with objects depending on their type, rather than the same operation for all objects as is often the case. For example, if you have a function that accepts an object type parameter (meaning that you can pass it just about anything), you might perform additional tasks if certain objects are encountered. Using a combination of GetType() and typeof() (a C# operator that converts a class name into a System.Type object) you can perform comparisons such as:

 if (myObj.GetType() == typeof(MyComplexClass)) { // myObj is an instance of the class MyComplexClass. } 

The System.Type object returned is capable of a lot more than this, but I won't cover this here. This topic is covered in more detail in Chapter 27.

It can also be very useful to override the ToString() method, particularly in situations where the contents of an object can be easily represented with a single human-readable string.

You see these System.Object methods repeatedly over the coming chapters, so I'll end this discussion for now and go into more detail as necessary.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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