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

Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
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