Click here to Skip to main content
12,818,785 members (30,529 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 27 Aug 2012

System.Object Members

, 27 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
System.Object members

System.Object Class Members

I recently started with my blog site and hence, I decided to post something from my old learning notes (of course after polishing it), thinking it could be useful to someone. I am going to talk about System.Object class. It is the ultimate base class for all types in CLR (except pointer types which are not convertible to object instance). This class defines methods which can be utilized by instances of value as well as reference types. Value types in CLR are those types which inherit from either System.ValueType or System.Enum and these two classes in turn derive from System.Object, therefore, all the methods defined in Object class are available to instances of value types as well as reference types. The Object class defines the following operations.

Note: It is important to note that unlike reference type instances, value type instances are stored sans object header in memory and hence, calling virtual methods on value type instances would result in the instance being boxed and so on. Boxing/Un-boxing is performance costly operation and hence, should be avoided.

public virtual bool Equals(object obj);

This method is defined as virtual and therefore, can be overridden by type deriving from object class. It should be overridden to define custom implementation for establishing equality with another object instance. In its default implementation, this method does a reference equality check for reference types and bitwise equality for value types.

Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.

A derived type might override the Equals method to implements the value equality. Value equality means the compared objects have the same value even though they have different binary representations. For example, consider two decimal objects that represent the number 1.10 and 1.1000. The decimal objects don’t have bitwise equality because they have different binary representations. However, the objects have value equality because the numbers 1.10 and 1.1000 are considered equal for the comparison purpose.

Usage Scenarios: You can use the equality operator (==) or call the Equals method to compare two instances for equality. The call to equality operator would be determined at the compile time depending upon the type of receiver and argument to it, whereas the call to Equals method is a virtual call and would be determined at runtime depending upon the type of receiver, and therefore, would impact the performance. Therefore, it would be advisable to use the equality operator where you wish to use the default CLR implementation for equality and Equals method should be called only when the type you are comparing for equality has its own implementation for Equals method.

Also, calling the Equals method on value types will have far more performance issues, as CLR would need to use reflection to determine the method to be called as value type instances don’t have object header like reference type instances and therefore, you should avoid calling virtual methods on value type instances.

public static bool Equals(object objA, object objB);

Similar to the earlier method in terms of functionality, but this variant is a static method and takes both the objects as parameters.

public virtual int GetHashCode();

This instance method returns the hash code for the current object.

Usage Scenarios: It is useful in situations where the object is being maintained in collections like HashTable or other data structure. A derived type can override this method and provide its own custom implementation.

public Type GetType();

It returns the System.Type instance for the current instance.

Usage Scenario: It is useful in situations where we need to pass the type instance as parameter or need to work with the reflection API.

protected object MemberwiseClone();

It’s a protected method, hence, available to all the classes which inherit from System.Object. This is useful in the cloning operation and the default implementation shallow copies the instance, i.e., creates bit-by-bit copies of value type members and reference type members are copied but the referred data is not.

public static bool ReferenceEquals(object objA, object objB);

It’s a static method which compares two references and returns true if the two point to same object instance.

public virtual string ToString();

It’s a virtual method which can be overridden by the derived class to provide one’s own custom description of an object instance. Ideally, a class must override this method to return some meaningful text which associates with the object instance.

P.S. In addition to blogging, I use Twitter to share tips, links, etc. My Twitter handle is: @girishjjain


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Girish J Jain
Technical Lead CitiusTech
United States United States
Girish Jain works on Microsoft .Net framework technologies and is a big fan of WPF, WCF, and LINQ technologies. When not spending time with family, Girish enjoys creating small tools, utilities, frameworks to improve developer productivity and also writes Windows Phone applications.

Loves economics, technology, family, and tennis. Pity liars and politicians.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
Klaus Luedenscheidt27-Aug-12 18:39
memberKlaus Luedenscheidt27-Aug-12 18:39 
GeneralMy vote of 4 Pin
Christian Amado27-Aug-12 10:17
memberChristian Amado27-Aug-12 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 27 Aug 2012
Article Copyright 2012 by Girish J Jain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid