<< Chapter < Page | Chapter >> Page > |
I will briefly discuss the default versions of some of the methods defined in the Object class, and will explain that in many cases, those default versions are meant to be overridden.
The Object type is a completely generic type that can be used to store a reference to any object that can be instantiated in C#.
The Object class defines the following eight methods , which are inherited by every other class:
Because these eight methods are inherited by every other class, they are always available for you to use in your code. (Possibly the most frequently used of these methods is the ToString method.)
Two of the methods in this list are defined in overloaded versions (same name, different formal argument lists) .
This author says "string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32."
Because every class is either a direct or indirect subclass of Object , every class in C#, (including new classes that you define) , inherit these methods.
Some of these methods are intended to be overridden for various purposes. This includes Equals , Finalize , GetHashCode , and ToString , which are all declared virtual .
However, some of them, such as GetType , are not declared virtual and therefore are intended to be used directly without overriding.
You can store a reference to any object in a reference variable of type Object .
Because every new class inherits these methods, you can call any of these methods on any reference to any object stored in a reference variable of type Object or in a reference variable of any other type.
If the class from which the object was instantiated inherits or defines an overridden version of one of the methods in the above list, calling that methodon the reference will cause the overridden version to be executed.
Otherwise, calling that method on the reference will cause the default version defined in the Object class to be executed.
The behavior described above is illustrated in the sample program named Polymorph04 , which you can view in its entirety in Listing 7 near the end of this module.
For purposes of illustration, this program deals specifically with the method named ToString from the above list, but could deal with the other virtual methods in the list as well.
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?