Understanding values and references

The System.Object class

One of the most important reference types in the .NET Framework is the Object class in the System namespace. To fully appreciate the significance of the System.Object class, you need to understand inheritance, which is described in Chapter 12, “Working with inheritance.” For the time being, simply accept that all classes are specialized types of System.Object and that you can use System.Object to create a variable that can refer to any reference type. System.Object is such an important class that C# provides the object keyword as an alias for System.Object. In your code, you can use object, or you can write System.Object—they mean the same thing.

In the following example, the variables c and o both refer to the same Circle object. The fact that the type of c is Circle and the type of o is object (the alias for System.Object) in effect provides two different views of the same item in memory.

Circle c;
c = new Circle(42);
object o;
o = c;

The following diagram illustrates how the variables c and o refer to the same item on the heap.

08fig04.jpg