Introducing C# and the .NET Framework - C# Source code
(Page 3 of 12 )
When you write an Application in C# the compiler doesn't generate a binary file (like traditional compilers) but rather it generates a file (for now you can call this file a .NET Module) that contains Microsoft Intermediate Language (MSIL) code, plus metadata (which we will discuss in the article on the Assemblies). So if you use Visual Basic.NET, the VB.NET compiler will generate MSIL, too -- and this is also the case for Managed C++ or any other .NET compliant language (more about .NET Compliant Languages later).
The Common Language Runtime (which is the execution environment for the .NET Framework Applications) has a compiler called Just-In-Time, or the JIT Compiler, which will compile the MSIL Instructions into native CPU Instructions upon the first execution of your application methods on a one-by-one basis. I think this needs a little more explanation.
When you write a C# application that contains only two methods, such as the following class:
class Class1
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.WriteLine("I like C# Programming");
}
}
then, when you compile this code, the C# Compiler will generate a file that contains the MSIL of the application. When you execute this application the Common Language Runtime will compile the MSIL Instructions into native CPU Instructions using the JIT Compiler on the first invocation for each method in your program.
So for the first call to Console.WriteLine("Hello World"); method will call the JIT Compiler to compile it to native CPU and save this native CPU version of the method to a block of memory, so the next call to this method will call this memory version, which guarantees fast performance.
The next statement calls the same method (Console.WriteLine()) with a different argument, so this will call the native CPU in-memory version of the method and pass it the string "I Like C# Programming." Note that when you terminate the execution of your application the in-memory version of your methods will not be available (it will be destroyed) and it will be created each time you execute your application. So the process is repeated every time you execute your application.
You may think that this two step compilation may slow the performance of your applications, but you should know that Microsoft spent a lot of time on optimizing this process. Before .NET, when you compiled an application, the compiler optimized the code but it optimized this code on the machine that you used to compile the application. So when you installed or deployed the application on your client machines, it would simply run but it would not be optimized. With the .NET Framework and the Common language Runtime, the application is compiled to MSIL Instructions, and when you run the application the JIT Compiler will optimize it depending on the current platform. So when you use, for example, a computer with a Pentium 4 processor, the JIT Compiler will know and actually optimize to this processor's features.
Next: The .NET Type Story >>
More C# Articles
More By Michael Youssef