Thursday, February 18, 2016

Object Class in .NET

In today's blog post, we will be discussing the System.Object class in .NET framework.

Object Class

Object class is the ultimate base class of all classes in the .NET framework. Whether its a reference type or value type or any custom type we define, all types inherit from Object class.

Objects in .NET
Since Object class is the universal base class, the methods defined in the Object class are available in every object in the system.

Here are the methods in the object class:

i) public Object() Constructor: This method is called when an object is created.

ii) public static bool Equals(Object objA, Object objB): static method to compare two objects. This method determines whether the specified object instances are considered equal.

iii) protected Object MemberwiseClone(): creates a shallow copy of the current object.

iv) public static bool ReferenceEquals(Object objA, Object objB): static method to check whether specified object instances are the same instance.

v) public Type GetType(): gets the Type of the current object.

vi) protected Object MemberwiseClone(): returns a shallow copy of the object by copying its members.

And here are some of the methods that one can override:
vii) public virtual bool Equals(Object obj): specifies whether the specified object is equal to the current object.

viii) public virtual int GetHashCode(): it returns the hash using the default algorithm. It should be overridden if needed depending on object's contents.

ix) public virtual string ToString(): returns the string representation of the object. For an object, it just returns the type name by default. It should be overridden based on object contents and usage.

x) Finalize(): it can be used to perform clean up operations before it is reclaimed by garbage collection.

Another reason why Object base class matters

Since every class has a base class of Object, we can use the Object type in collections or method parameters to accept any object. For example, if we say:

List<Object> objList;

it can accept List of any object types.


Method1(Object obj);

it can accept any object as the parameter.

In both the cases, any object can be used but there might be added cost of boxing and unboxing involved. That can lead to performance problems.
For the latter case, if we want a variety of objects to be handled by the method, we can create overloads of the method with specific object types while keeping the Object parameter overload as well. In that case, if an exact match is found, it's used, otherwise, the method with Object parameter is used.
Another way to avoid the boxing and unboxing performance issue is to use generics. I have talked about Generics previously here.


Since object class is a universal base class, it's important to understand the methods available to us. These methods are available to us in all the objects and we can override some of them to implement the functionality we need. Also, while using it in methods or collections can be useful to us, we need to consider the performance cost that might come along.

For future updates to my weekly blog, please subscribe to my blog via the "Subscribe To Weekly Post" feature at the right and follow me on Twitter. Until then Happy Coding :)

No comments:

Post a Comment