Class Object

Object is the base class for all derived classes. It provides some methods and capabilities in the C# language. All derived types can be referenced through the object base type. With careful casting, we access functionality on further derived types.
Classes can be designed specifically as building blocks for other types, and existing classes can be examined to see if they exhibit similarities that can be exploited in a common base class. Stroustrup, p. 39
To begin, this program demonstrates how any type can be considered an object in the C# language. This is because each class is derived from the base class object. However, the program also shows that as an object, the reference still is an instance of the more derived StringBuilder type.
Program that uses object [C#]
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
Next, this program introduces a method that receives an object type parameter to demonstrate how to use objects as arguments to functions. As a reminder, the object keyword is actually 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.
Program that uses object type argument [C#]
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
Overview. The string variable is assigned a reference to a literal string, and this is passed as an object type to the Test method. The Test method then tests its parameter against null, and then uses the 'is' cast to test the most derived type of the object parameter. The first two calls to Test will evaluate the (value is string) expression as true. The second two calls will match (value is int).
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.Equals object.ReferenceEquals
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. This article provides an example of how you can create and use object arrays.
object[]
It is possible to use the object type in the formal parameter list, but this will incur a loss of type information. If you do use object in the formal parameter list, you will have to try to determine the derived type of the object. You can use the 'is' or 'as' casts for this.
Is Cast As CastIn the C# language, 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 the object type. Class hierarchies in object-oriented programming allow us to reference derived types by their base type. Mathematically, this is transitive closure.
Transitive Closure
In the C# language, the object type is a typical type in most aspects, although it has some complexities regarding the implementation. For example, 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 can be considered more a logical instead of physical derivation in this case. 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. This is termed boxing; the opposite process is termed unboxing.
Unboxing
We examined the object type in the C# language. The program examples illustrate how to use an object as an argument; how to receive any type as an object type in the formal parameter list; and how to cast objects. In addition, we mentioned implementation-specific issues related to performance for value types.