Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redundant to inherit from Object in C#?

Tags:

c#

.net

As stated above, is it redundant to inherit from Object in c#? Do both sets of code below result in equivalent objects being defined?

class TestClassUno : Object {     // Stuff } 

vs.

class TestClassDos {     // Stuff } 

I snooped around on MSDN but wasn't able to find anything perfectly conclusive.

like image 434
Keplah Avatar asked May 24 '12 16:05

Keplah


People also ask

Is it possible to inherit from object?

They behave like Objects, they can be added to collections of type Object, they can use any method defined in Object. So, YES, everything (except primitives) inherit from Object in Java.

Do all classes inherit from object c#?

C# classes don't require to declare the inheritance from the Object class as the inheritance is implicit. Every method defined in the Object class is available in all objects in the system as all classes in the . NET Framework are derived from the Object class.

Why inheritance is used in C#?

In C#, inheritance allows us to create a new class from an existing class. It is a key feature of Object-Oriented Programming (OOP). The derived class inherits the fields and methods of the base class. This helps with the code reusability in C#.

What is used as inheritance operator in CS?

The symbol used for inheritance is :. Syntax: class derived-class : base-class { // methods and fields . . } Example: In below example of inheritance, class GFG is a base class, class GeeksforGeeks is a derived class which extends GFG class and class Sudo is a driver class to run program.


2 Answers

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 } 
like image 163
JaredPar Avatar answered Sep 22 '22 20:09

JaredPar


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

like image 37
Aren Avatar answered Sep 21 '22 20:09

Aren