Redundant to inherit from Object in C#?

Yes, everything ultimately inherits from an object if defined as class. Leave the explicit inheritance out of your code.


If left unspecified every class definition will implicitly inherit from System.Object hence the two definitions are equivalent.

The only time these two would be different is if someone actually defined another Object type in the same namespace. In this case the local definition of Object would take precedence and change the inheritance object

namespace Example {
  class Object { } 
  class C : Object { } 
}

Very much a corner case but wouldn't point it out if I hadn't seen it before

Note that the same is not true if you used object instead of Object. The C# keyword object is a type alias for System.Object and hence it wouldn't match Example.Object.

namespace Example2 { 
  class Object { } 
  class C : Object { } // Uses Example.Object
  class D : object { } // Uses System.Object
}

Of course if you have a truly evil developer you could still cause confusion with object

namespace System { 
  class Object { 
    private Object() { } 
  }
}

namespace Example3 {
  // This will properly fail to compile since it can't bind to the private
  // Object constructor.  This demonstrates that we are using our definition
  // of Object instead of mscorlib's 
  class C : object { } // Uses our System.Object
}

Tags:

C#

.Net