4

All classes that we create inherit from the Object class without needing to explicitly declare inheritance to that class.

  • How does Microsoft can make all .NET class implicitly inherit from the Object class?
  • Does the compiler inject that inheritance behind the scenes?
  • Can I do the same kind of thing: make all my classes inherit implicitly from a class interface for example?

My question may be ambiguous so I will clarify:

I'm not asking about inheriting from another class other than Object since I know that multiple inheritance for classes is forbidden. I'm talking about inheritance rather through class Interfaces, my question is about IMPLICITLY DOING IT not about substituting an object by another class of my own, or do multiple inheritance with Object and another class of my own. I want to get all classes inherit form Interface1OfMyOwn and Interface2OfMyOwn.

Brian Lyttle
  • 14,558
  • 15
  • 68
  • 104
user310291
  • 36,946
  • 82
  • 271
  • 487
  • 1
    may be you can modify the `T4` template that is used to create a class and make all classes derive from your custom base class. – Saravanan May 26 '13 at 11:47
  • _All_ classes _do_ implicitly inherit Object. You can _Explicitly_ inherit from object. (Note that some other net languages don't support multiple inheritance like VB.Net). The Object class is special in that _everything_ in .Net is an object. To have everything inherit an arbitrary object is not possible to my knowledge but can be handled as described by @saravanan – Basic May 26 '13 at 11:49
  • @saravan Microsoft does not use T4 as far as I know :) – user310291 May 26 '13 at 11:51
  • @Basic As far as I know, the entire .NET CLR lacks support for multiple inheritance, so there are no languages that support it either. C# certainly doesn't. – Cody Gray - on strike May 26 '13 at 11:52
  • @Basic that I know. My question is about NOT to do EXPLICIT inheritance because that's cumbersome. Above all I want to know how Microsoft does it and if we could do the SAME way. – user310291 May 26 '13 at 11:52
  • @CodyGray - I seem to remember reading that in the Jefferey Richter book but [just came across this article](http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/why-doesn-t-c-support-multiple-inheritance.aspx) that says it is possible. – Martin Smith May 26 '13 at 11:53
  • @Cody I think Objective C does somehow ? thanks to dynamic typing, you can also provide much of the function of multiple inheritance using OpenStep's implementation of NSObject. http://support.apple.com/kb/TA45894 – user310291 May 26 '13 at 11:53
  • Objective C doesn't have anything to do with .NET. C++ supports multiple inheritance, too. Also irrelevant here. – Cody Gray - on strike May 26 '13 at 11:53
  • @Cody yes in .net but in fact I am interested to get the answer for any mobile platform language. – user310291 May 26 '13 at 11:55
  • 3
    @user310291 Could you explain your problem in details? Why do you need this feature? And why explicit inheritance is cumbersome? Do you have over 9000 classes? – mixel May 26 '13 at 11:55
  • @mixel My question is not about multiple inheritance, it's about creating framework with a lot of class interfaces for OTHER people who hate frameworks and will never agree to use it if they have to remember all the class interfaces. But no matter just take my question for curiosity sake. – user310291 May 26 '13 at 11:59
  • I don't know what that means. You don't have to remember the interface of all the classes to use a framework. And "default" inheritance certainly won't help to make the code any less confusing or easier to use. So now I'm even *more* confused about what you're asking here. – Cody Gray - on strike May 26 '13 at 12:00
  • @Cody : of course as user have to know what Interface he needs to implement to get the kind of feature you want. In fact it's so cumbersome Microsoft did redesign some of their framework without them. – user310291 May 26 '13 at 12:01
  • 1
    Confused too :) Class interface is it interface or class? When I use class from any framework I write explicitely var a = new FooFrameworkClass(); because I know what kind of class I want. Do you suggest that using your framework in my project makes all classes in my project inherit from some class in your framework? – mixel May 26 '13 at 12:06
  • 1
    @mixel an interface is a special class without implementation on its own but it's still a class :) – user310291 May 26 '13 at 12:10
  • 2
    Notable: types are not required to inherit from `System.Object`. Writing direct `MSIL` allows you to skip this behavior. See: http://stackoverflow.com/a/542039/1688785 Probably just a convenience. – Caramiriel May 26 '13 at 12:20
  • 1
    @Caramiriel They are required to do so as per the spec. The comment threads on the answer you linked has more details. Writing directly in MSIL allows you to shoot yourself in the foot, not that big of a surprise. – Cody Gray - on strike May 26 '13 at 12:29

4 Answers4

12

All classes implicitly extend Object because the C# standard specifies the language so.

Asking how they implemented that is like asking how they implemented the fact that a void method doesn't return data, or how they implemented the fact that classes have properties: It's simply somewhere in the code of the compiler (which here may be the C# compiler or the JIT compiler). We can't know for sure, but it's plausible to say that whenever the compiler doesn't detect explicit inheritance in a class, it just goes for System.Object instead, simple as that. So you'll have to accept that Object is simply a special type for the compiler.

Why you can't do that with a custom class seems obvious: It would thoroughly confuse the compiler over whether a class with no explicit inheritance should extend Object or your custom class. If it chose the latter for every class with no explicit inheritance, it would introduce compilation errors in most cases or, if you're particularly unlucky, surprising behavior that only manifests at runtime. If it chose the former and you'd have to explicitly opt-in your classes for this, what's the point?

(The same, of course, would happen if you wanted to implicitly implement an interface: All classes that don't really implement that interface would break and cause compilation errors. Or, if you're unlucky, the interface would match unrelated methods in a class that happen to have a matching signature and cause strange behavior that you'd find out about by testing.)

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
  • 1
    That's exactly what it does. And imagine how confusing things would get when it started deriving BCL classes from your custom class because they did not specify an explicit base class! – Cody Gray - on strike May 26 '13 at 12:00
  • I'm not asking about inheriting from another class that Object since I know perfectly that multiple inheritance for class is forbidden, I'm talking about inheritance rather through class Interfaces, my question is about IMPLICITELY DOING IT not about substituing object by another class of my own or do multiple inheritance with Object and another class of my own. I want to get all class inherit form Interface1OfMyOwn and Interface2OfMyOwn. – user310291 May 26 '13 at 12:06
  • 1
    @user310291 I've already addressed your first question of how it's *probably* implemented for `System.Object`. I've also edited the answer to highlight the answer to your second question, why there's no implicit interface inheritance. – Theodoros Chatzigiannakis May 26 '13 at 12:14
  • Isn't this done by the .Net Framework, rather than by the individual compilers themselves? Or does the Framework merely require that the compilers implement it this way? – RBarryYoung May 26 '13 at 12:30
  • 1
    @RBarryYoung You're right, the CIL standard does enforce this, even if the AOT compiler doesn't. In this sense, the term "compiler" may refer to the JIT as well (it doesn't see an explicit inheritance in the IL, so it goes for `Object`). – Theodoros Chatzigiannakis May 26 '13 at 12:47
7

This is supported at a very low level within the runtime, a value of a value type can be embedded in an object that's stored on the garbage collected heap. Thus turning a value type in a reference type and creating the illusion that every value type inherits from ValueType and Object. Which are reference types.

The mechanism is calling boxing in .NET, the value type value literally gets "boxed" into an object. And there's an unboxing conversion to go back from the object with the boxed value to a value of a value type. The C# compiler will emit these conversions automatically based on your source code. There are dedicated opcodes for this is IL, the Intermediate Language that the C# compiler emits from your source code. Respectively the Opcodes.Box and Opcodes.Unbox instructions. Opcodes.Constrained is an instruction that can optimize the conversion. The jitter knows how to implement them and generates very efficient inline machine code to make these conversions.

Boxing is a highly specific to System.Object being a base class in the type hierarchy and the plumbing that supports it is highly specific to value type values. It is not an extensible mechanism, you cannot add your own IL instructions nor extend the jitter nor give the C# language new syntax. If you need your types to have a common base interface or class then you have to declare them that way in your code. The dynamic keyword may be attractive to you, it isn't clear from the question.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

I agree completely with both answers provided... just wanted to add an option what you can do to achieve the desired outcome i.e. "automatatically inheriting from custom interfaces":

You can gain a very deep control of the compilation process with the Roslyn project from MS, but be warned that Roslyn is still not "production-ready".

You could write some code to influence the compilation process and analyse and even change how the compiler works to achieve what you want.

Another option worth looking into might be AOP - there are some .NET frameworks out there that can do amazing things (see here for example).

Yahia
  • 69,653
  • 9
  • 115
  • 144
1

For what you want, Extension Methods on object will work. You can also work with a generic type, which loses less type information straightaway.

static class GeneralMethods
{
    public static void Testing(this object This)
    {
        This.ToString().Dump("Testing");
    }

    public static void Test2<T>(this T This)
    {
        This.Dump("Test2");
    }

}
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101