Is everything in .NET an object?

The problem here is that this is really two questions – one question is about inheritance, in which case the answer is “nearly everything”, and the other is about reference type vs value type/memory/boxing, which case the answer is “no”.

Inheritance:

In C#, the following is true:

  • All value types, including enums and nullable types, are derived from System.Object.
  • All class, array, and delegate types are derived from System.Object.
  • Interface types are not derived from System.Object. They are all convertible to System.Object, but interfaces only derive from other interface types, and System.Object is not an interface type.
  • No pointer types derive from System.Object, nor are any of them directly convertible to System.Object.
  • “Open” type parameter types are also not derived from System.Object. Type parameter types are not derived from anything; type arguments are constrained to be derived from the effective base class, but they themselves are not “derived” from anything.

From the MSDN entry for System.Object:

Supports all classes in the .NET
Framework class hierarchy and provides
low-level services to derived classes.
This is the ultimate base class of all
classes in the .NET Framework; it is
the root of the type hierarchy.

Languages typically do not require a
class to declare inheritance from
Object because the inheritance is
implicit.

Because all classes in the .NET
Framework are derived from Object,
every method defined in the Object
class is available in all objects in
the system. Derived classes can and do
override some of these methods.

So not every type in C# is derived from System.Object. And even for those types that are, you still need to note the difference between reference types and value types, as they are treated very differently.

Boxing:

While value types do inherit from System.Object, they are treated differently in memory from reference types, and the semantics of how they are passed through methods in your code are different as well. Indeed, a value type is not treated as an Object (a reference type), until you explicitly instruct your application to do so by boxing it as a reference type. See more information about boxing in C# here.

Leave a Comment