Tuesday, May 19, 2009

Overview of the Object Class

Introduction

The object class is the ultimate base class of every type. Because of its lofty position and significant influence on the code you write, you should have a good understanding of the object class and its members. You may also be interested in knowing why object exists in the first place. In this article, we look at these things with the goal of making you more aware of the rationale and use of the object class in your code.

Why an Object Class?

As the ultimate base class that all other other types directly or indirectly derive from, the object class allows you to build generic routines, provides Type System Unification, and enables working with groups of types as collections. Common practice for implementing generic routines has been to write a method that accepts arguments of type object and allow users to pass in any type they want. This approach is difficult to maintain because of the lack of type safety and inefficiency for value types. I believe the practice of using the object class for generic routines will be replaced in many future scenarios by the use of Generics in C# v2.0.
Reference types inherit the object class either directly or through other reference types. Value types inherit implicitly from the object class through System.ValueType. In C#, the object class is a language specific alias for the .NET Base Class Library (BCL) type with the fully qualified name of System.Object.

The ability of both reference types and value types to be treated as objects supports Type System Unification. In many languages, built-in types such as int and float have no object-oriented properties and must be explicitly wrapped in objects to simulate object-oriented behavior. However, C# eliminates the requirement to wrap built-in types through the concept of value types that inherit from System.ValueType, which inherits from System.Object. This allows C# built-in types to be worked with in a manner similar to reference types. From an object-oriented perspective, under Type System Unification both reference types and value types are objects.

Of course you don't get Type System Unification for free. When assigning a value type to an object, behind the scenes, the system performs an extra operation referred to as Boxing. After a boxing operation has completed, a value type is considered boxed. Similarly, when copying a boxed value type back to a normal value type, behind the scenes, the system performs another operation referred to as Unboxing. While any value type can be boxed, it is not logical to assume that any reference type can be unboxed. Only objects that have been previously boxed can be unboxed. When a value type is boxed, a new object for that type is created in memory and the value of the value type is copied into that object. During unboxing the value in the object is copied into the value of a value type.

Boxing and Unboxing
int myValType1 = 5;
object myRefType = myValType1; // myValType is boxed
int myValType2 = (int) myRefType; // myValType is unboxed

Although, for performance reasons, you need to be aware of the boxing/unboxing operations, the point being made here is that the object class, as the ultimate base class, promotes Type System Unification. This makes it very simple to work with all types in the same manner for those times when such behavior in your application is necessary.

The object class also facilitates using any type in a collection. It contains methods that can be reused or overridden in base types. Since collections, such as Hashtable and ArrayList accept object types, you can add any type to them. The Equals, GetType, and GetHashCode object class methods are called directly by the collection classes. Other object class methods offer general capabilities that types have access to and can expose in their interface.


Conclusion

As the ultimate base class of all .NET types, the object class is very important. It contains several methods that you need to be aware of. You need to know how to use and implement overrides of some of these methods as appropriate for building well-behaved custom types. Most of the discussion about object class methods begs for more information, which I'll follow up with in subsequent articles. This has been an overview that illuminates key points that should be useful in your development endeavors.

No comments:

Post a Comment