Before talking in more detail about these, a few bits of terminology need to be clarified.
- Managed Code
- "Managed Code" is code that targets .NET, and which contains certain extra information — "metadata" — to describe itself. Whilst both managed and unmanaged code can run in the runtime, only managed code contains the information that allows the runtime to guarantee, for instance, safe execution and interoperability.
- Managed Data
- With Managed Code comes Managed Data. CLR provides memory allocation and deallocation facilities, and garbage collection. Some .NET languages use Managed Data by default — C#, Visual Basic.NET, JScript.NET — whereas others — C++ — do not. Targetting CLR can, depending on the language you're using, impose certain constraints on the features available; for instance, C++ loses
multiple inheritance. As with managed and unmanaged code, one can have both managed and unmanaged data in .NET applications — data that doesn't get garbage collected but instead is looked after by unmanaged code.
- Common Type System
- The CLR uses something called the Common Type System (CTS) to strictly enforce type-safety. This ensures that all classes are compatible with each other, by describing types in a common way. CTS defines how types work within the runtime (their declaration and usage), which enables types in one language to interoperate with types in another language, including cross-language
exception handling. As well as ensuring that types are only used in appropriate ways, the runtime also ensures that code doesn't attempt to access memory that hasn't been allocated to it (that is to say, the code is type-safe).
- Assemblies
- .NET programs are constructed from "Assemblies". An Assembly is a compiled and versioned collection of code and metadata that forms an atomic functional unit. All Assemblies contain a Manifest, which contains the Assembly name, version, and locale, has a list of files that form the Assembly, what dependencies the Assembly has, and what features are exported by the Assembly.
When you target .NET with your compiler, what gets generated isn't native code. Instead, it's a small PE wrapper around three blocks of data. PE (Portable Executable) is the binary format used to contain Win32 programs.
PE files contain a stub MS DOS program (if you've ever tried to run a Win32 executable from DOS and seen the "This program requires Microsoft Windows" message, you've seen that stub in action). .NET binaries will then contain a Win32 stub — to either use .NET to run the actual program, or to say something like, "This program requires .NET to run" if it isn't available.
The first block of data within the PE wrapper is the IL itself. IL looks approximately like assembler. This is the bit that actually gets compiled and executed.
The second is called the metadata. This describes the contents of the file — for instance, what methods it provides, what parameters they take, and what they return.
The third is the manifest. This describes what other components the executable needs in order to run. It also contains public keys of external components, so that the CLR can ensure that the external component is the one that the executable thinks it is.
When running the executable, the CLR uses Just-In-Time compilation. As each method within the executable gets called, it gets compiled to native code; subsequent calls to the same method don't have to undergo the same compilation, so the overhead is only incurred once.
JIT compilation raises some issues. One is that it demands resources — memory and processor particularly. To solve this, MS have two JIT compilers: a normal one, that optimizes compiled code fairly well, but can be processor and/or memory intensive; and an "EconoJIT". EconoJIT might not optimize the code as well, but it'll require less memory and processor time to run. It will also allow your running program to discard the compiled form of a method, thereby freeing up memory, at the expense of having to compile it again. A third kind of compiler has been hinted at (though hasn't made it into the version 1 release), called the "OptJIT". It will work a little differently; it will use a subset of IL (OptIL) with additional information to suggest to the OptJIT compiler how to generate its output; there may also be greater optimization of the IL. The idea is to reduce the overhead due to the JIT compilation without sacrificing the quality of the emitted machine code.