TheDeveloperBlog.com


C# Object Type

Object is the base class for all derived classes. It provides some methods and capabilities. All derived types can be referenced through the object base type. With careful casting, we access functionality on further derived types.


Intro. To begin, this program shows how any type can be considered an object in the C# language. This is because each class is derived from the base class object. Each reference is both an object and its derived types.

Next: The program shows that as an object, the reference still is an instance of the more derived StringBuilder type.

C# program that uses object

using System;
using System.Text;

class Program
{
    static void Main()
    {
	// Use an object reference.
	object val = new StringBuilder();
	Console.WriteLine(val.GetType());
    }
}

Output

System.Text.StringBuilder


Parameter. Next, this program introduces a method that receives an object type parameter. It shows how to use objects as arguments to functions. As a reminder, the object keyword is aliased to the System.Object type in the base class library.

The program passes a string directly to an object parameter slot in the Test method, and then casts the string to an object directly. It also uses an int variable as an object, as well as the null literal.

Null
C# program that uses object type argument

using System;

class Program
{
    static void Main()
    {
	// Use string type as object.
	string value = "Dot Net Perls";
	Test(value);
	Test((object)value);

	// Use integer type as object.
	int number = 100;
	Test(number);
	Test((object)number);

	// Use null object.
	Test(null);
    }

    static void Test(object value)
    {
	// Test the object.
	// ... Write its value to the screen if it is a string or integer.
	Console.WriteLine(value != null);
	if (value is string)
	{
	    Console.WriteLine("String value: {0}", value);
	}
	else if (value is int)
	{
	    Console.WriteLine("Int value: {0}", value);
	}
    }
}

Output

True
String value: Dot Net Perls
True
String value: Dot Net Perls
True
Int value: 100
True
Int value: 100
False

The string variable is assigned a reference to a literal string. This is passed as an object type to the Test method. Test then tests its parameter against null. It then uses the is-cast to test the most derived type of the object.

Next: The first two Test calls will evaluate the (value is string) expression as true. The second two calls will match (value is int).

StringsInt

Derived types. Each type is logically derived from the object type. If you create a class and do not specify what it derives from, it is implicitly derived from object. We don't have a choice in this matter.

Note: Class hierarchies in object-oriented programming allow us to reference derived types by their base type.


Equality. The object type provides the Equals and ReferenceEquals methods. These help us understand how objects are compared for equality, and how the data in an object is different from its reference.

object.Equalsobject.ReferenceEquals

Clone. Cloning is not just a subject of science fiction movies. It can be done with the Clone method in the C# language. The object type provides the Clone method. Its syntax is somewhat difficult.

Clone

Arrays. You can use arrays of objects and pass these arrays around in your programs. Object arrays are necessary for some types in System.Data or Microsoft.Office.Interop.Excel. We explore how you can create and use object arrays.

Object Array

Cast parameters. It is possible to use the object type in the formal parameter list, but this incurs a loss of type information. In the method, you will have to try to determine the derived type of the object. You can use the is-cast or as-cast for this.

IsAs

Boxing. The object type has some complexities regarding its implementation. Because of optimization concerns, value types do not actually cause heap allocations, and without the heap allocation, the object cannot actually exist.

Therefore, the object type is more a logical instead of physical derivation. Because of this lack of a type pointer on value types, the value types must be allocated in a new object to be cast to their base object type.

Note: This is termed boxing. The opposite process is termed unboxing. Both of these operations have performance impact.

Unboxing

Summary. We examined the object type. The program examples illustrate how to use an object as an argument. They show how to receive any type as an object type in the formal parameter list. And they also cast objects.

Further: We covered implementation-specific issues related to performance for value types.