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

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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