3.2.8 How to Utilize the "Object" Class

Every object in C# inherits from a class called Object. You will never "see" a class explicitly inheriting from the Object class, they simply do by default. The Object class contains these members:

Note that some of these members are virtual, meaning they can be overridden.


You have probably seen some of these methods when using IntelliSense (because all objects inherit from Object).

If we isolate the object with casting we can see it clearer.

So what do these methods do? In the image below there are example of what is returned from each of the methods. Equals returns a boolean value of whether two objects are the same, GetHashCode returns a hash code, GetType returns a Type object with data, and ToString returns a default string of the namespace and object name.

Most of the members of the Object class can be overriden so that we can provide our own functionality. Let's look at the ToString method. The ZooScenario.Employee description that was returned does tell us much. So in the Employee class we can override the ToString method like this:

Now when we use the ToString method on an Employee object we will get back a much cleaner description.