I’m starting to read in-depth on the .NET framework, and its Common Language Runtime. I’m reading a .NET overview by Microsoft and I’m not sure what is meant by this statement.
Code that targets the runtime is known
as managed code, while code that does
not target the runtime is known as
unmanaged code.
How do you target the Common Language Runtime? I know its defaulted when developing in Visual Studio, but how would you specifically target the CLR?
What code when developing a .NET application wouldn’t target the CLR, and thereby be called unmanaged code?
Targetting the runtime means compiling for the runtime, that is, among other things, outputting CIL aka MSIL (as opposed to x86/x64/etc. machine code).
Saying that anything that doesn’t target the runtime is “unmanaged code”, is a bit of a false dichotomy. Code running on the JVM for example is arguably managed, but I think we can take in this context the sentence to mean “unamanged [by the CLR]”.
It would be possible to write a C# compiler that could spit out native code and not target the runtime (albeit you’d probably need some runtime for GC at least), and it is equally possible to write C compiler that spat out CIL. In this example the hypothetical C# compiler wouldn’t be targetting the runtime but the hypothetical C compiler would be. The important distinction here is separating the language from its target.
A .NET application that didn’t target the runtime would be a contradiction. If it didn’t target the runtime it wouldn’t be a .NET application.
It can get fuzzier though, with
unsafeandP/Invoke. When you use functionality likeP/Invokeor COM interop, you end up targeting the runtime and additionally some other stuff. This doesn’t mean you’ve stopped targeting the runtime though, it just means you have additional dependencies beyond the runtime. Keeping track of this sort of stuff is why things like theCLSCompliantAttributeexist.