user7030548
user7030548

Reputation:

How compiler knows whether the running code is Managed or UnManaged in .NET framework

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!! :)

Upvotes: 3

Views: 198

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

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.

Upvotes: 3

Related Questions