3

When reading about the basics of .NET like Managed and Unmanaged code, the code which runs under CLR and developed in .NET framework is managed while unmanaged does not run under CLR and developed outside the .NET framework. Moreover, thinking and knowing about the practicalities about the codes and how they run and how the compiler knows the type of code was a bit confusing. So, to get rid of this confusion, felt to ask this on here.

Please let me know about this.

Thanks in advance!! :)

  • which compiler? as far as csc is concerned, it is always targeting a managed platform (MSIL). That IL *might* be converted later (IL2CPP etc). Or do you mean the C++ compiler? – Marc Gravell Mar 11 '19 at 08:34
  • @MarcGravell Nope not C++ compiler. Compiler within .NET –  Mar 11 '19 at 08:49

1 Answers1

3

If you're talking about the compilers for languages such as C#, VB(.NET) etc, then: from the compiler's perspective they are always managed. Everything they output is managed code, i.e. IL. There are places where C# etc get close to directly unmanaged - for example:

  • when using unsafe code and "unmanaged pointers", C# can talk directly to unmanaged memory - but it always expresses how to do that via IL (IL contains operators for dealing with raw pointers)
  • P/Invoke layers can express boundaries for talking to external unmanaged code

However, in both cases it is the JIT compiler at runtime (or the AOT compiler if using a non-JIT target platform) that interprets these unmanaged intents expressed in IL, and outputs the CPU intrinsics to implement them. But... that's exactly what the JIT / AOT does for all IL, so ... nothing different there.

So: from the perspective of a compiler for a language such as C# / VB(.NET), there is no actual question here: it is always working 100% managed in terms of the output - and since the compilers are (since Roslyn) also .NET code, they are 100% managed in terms of their actual execution too.

If you meant something different, it would be worth clarifying the question, being really, really specific about what scenario(s) you have in mind, preferably with examples.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for the info. Moreover, could you please elaborate on the flow how managed code differs from unmanaged while executing [pictorial if possible]. –  Mar 11 '19 at 13:07
  • 1
    @PPB you asked about the compiler, so ... that isn't relevant. And at runtime, the purpose of the JIT is to turn IL into opcodes suitable for the CPU, so... the JIT output is just regular opcodes. Again, the question you're asking doesn't really make sense. So I'm curious as to what it is that you *actually* want to know... – Marc Gravell Mar 11 '19 at 13:16