How does Microsoft make all .NET classes implicitly inherited from the Object class? Can I do the same?

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

  • How can Microsoft force an entire .NET class to implicitly inherit from an object class?
  • Does the compiler include scene inheritance?
  • Can I do the same thing: make all my classes inherit implicitly from the class interface, for example?

My question may be ambiguous, so I will clarify:

I do not ask about inheritance from a class other than Object, since I know that multiple inheritance is forbidden for classes. I am talking about inheritance, but through the Interfaces class, my question is NOT WRONG TO USE this is not about replacing an object with another class of my own or inheriting several objects with Object and another class. I want all classes to inherit the form Interface1OfMyOwn and Interface2OfMyOwn.

+4
source share
4 answers

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

Ask how they are implemented, how to ask, how they implemented the fact that the void method does not return data, or how they implemented the fact that classes have properties: it's just somewhere in the compiler code (which could be a C # compiler or JIT compiler). We cannot know for sure, but we can say that whenever the compiler does not detect explicit inheritance in a class, it just works for System.Object instead. So you have to accept that Object is just a special type for the compiler.

Why you cannot do this with a custom class seems obvious: it would completely confuse the compiler over whether the class should extend Object or your own class without explicit inheritance. If he chooses the latter for each class without explicit inheritance, he in most cases introduces compilation errors or, if you are particularly unlucky, an amazing behavior that appears only at runtime. If he chose the first, and you have to explicitly choose for his classes for this, what is the point?

(The same, of course, will happen if you want to implicitly implement an interface: all classes that do not actually implement this interface will not break and will not lead to compilation errors. Or, if you are not lucky, the interface will correspond to unrelated methods in a class that have a consistent signature and cause strange behavior that you learn about when testing.)

+11
source

This is kept very low at runtime; a value of type value can be embedded in an object that is stored in the garbage heap. Thus, reversing the value type in the reference type and creating the illusion that each value type inherits from ValueType and Object. What are the reference types.

The mechanism calls boxing in .NET, a value of type value literally gets "boxed" into the object. And there is unboxing conversion to return from an object with a short value to a value type value. The C # compiler will automatically convert these conversions based on the source code. There are dedicated operation codes for this; this is IL, an intermediate language that the C # compiler emits from your source code. According to the instructions of Opcodes.Box and Opcodes.Unbox. Opcodes.Constrained is an instruction that can optimize the conversion. Jitter knows how to implement them, and creates very efficient native machine code to perform these transformations.

Boxing is very specific for System.Object, which is the base class in the type hierarchy, and the plumbing that supports it is very specific for value type values. This is not an extensible mechanism, you cannot add your own IL instructions, nor can you increase jitter and give new C # syntax. If you need your types in order to have a common base interface or class, then you must declare them that way in your code. A dynamic keyword may be appealing to you; this is not clear from the question.

+6
source

I completely agree with both answers ... I just wanted to add the option of what you can do to achieve the desired result, that is, "automatically inherit from user interfaces":

You can get very deep control over the compilation process with the Roslyn project from MS , but be careful that Roslyn is still not a "-Ready production".

You can write some code to influence the compilation process, analyze and even change the way the compiler works to achieve what you want.

Another opportunity to look for might be AOP - there are some .NET environments that can do amazing things (for example, here ).

+3
source

Why do you need Extension methods on object . You can also work with a generic type that immediately loses type information.

 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"); } } 
+1
source

Source: https://habr.com/ru/post/1482811/


All Articles