<< 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.

References of type Object

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#.

Methods in the Object class

The Object class defines the following eight methods , which are inherited by every other class:

  • Equals - Overloaded. Determines whether two Object instances are equal.
    • Equals(Object) - Determines whether the specified Object is equal to the current Object .
    • Equals(Object,Object) - Determines whether the specified Object instances are considered equal.
  • Finalize - Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
  • GetHashCode - Serves as a hash function for a particular type.
  • GetType - Gets the Type of the current instance.
  • MemberwiseClone - Creates a shallow copy of the current Object .
  • ReferenceEquals - Determines whether the specified Object instances are the same instance.
  • ToString - Returns a String that represents the current Object .

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) .

The difference between string and String

This author says "string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32."

Every class inherits these methods

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.

To be overridden...

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.

Calling methods of the Object class

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.

And the behavior will be...

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.

Preview

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.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask