Introducing C# and the .NET Framework - The Common Language Runtime
(Page 6 of 12 )
The concept of the Runtime Environment is not new. Developers used to programming using Microsoft Foundation Classes (MFC) know that a runtime environment is needed to manage their applications. Java also has a runtime environment (the Java Virtual Machine), but C#, Visual Basic.NET, Managed C++ and all the other Microsoft.NET Compliant Languages have only one runtime environment between them, which is the Common Language Runtime. The CLR is the part of the .NET Framework that executes and manages any .NET Application, and as you know, the code produced by all .NET Compliant languages is MSIL and it's called managed code.
The CLR understands only MSIL (not C#, not Visual Basic.NET) so whatever the language has been used to produce the MSIL Instructions is not an issue for the CLR. The Interesting thing about the CLR is that it's loaded automatically each time you execute a .NET application, and it's represented as an assembly file (an assembly is a unit of deployment in .NET) called mscoree.dll (stands for Microsoft Common Object Execution Engine). It is needed on the machines that will run a .NET application, so you need to make certain your clients have the CLR installed. You can install it using the .NET Framework distribution components.
I like to think of the CLR as a monitor, guard and a supervisor for all .NET Applications. I say that because it's responsible for:
- Compiling the MSIL instructions to native CPU instructions. As you know, .NET language's compilers produce files that contain MSIL code, and for execution of your application the CLR uses its JIT Compiler to compile the MSIL Instructions to native CPU instructions, and to optimize these instructions to utilize the process features.
- Managing memory automatically for your applications using a Garbage Collection mechanism. This guarantees that objects that have no reference in your application will be freed, and you as the developer will not worry about releasing memory for objects. This guarantees faster performance and, at the same time, gives you the time to focus on the development issues of your applications.
- Handling application errors when required by throwing an Exception.
- Guaranteeing that your .NET Applications will run in a safe context by using a mechanism called Code-Access Security. There is another security mechanism called Role-Based Security which controls the access to system recourses. Using .NET Security that is managed by the CLR, you can develop very secure applications.
- Knowing what files are needed to execute your application (these are the assemblies that represent the .NET Framework Class Library). It uses an algorithm to search for these files. The simplest C# program must refer to and use one assembly that contains most of the basic functionality, such as methods to write and read lines from and to the Console.
- Beginning a process called Code Verification. When the JIT Compiler begins to compile the MSIL Instructions to native CPU Instructions, this process checks type safety. In other words it ensures that your code doesn't make any illegal operations or allow any illegal access, and this is where the expression that "C# is a Type-Safe Language" comes from.
Next: The Common Type System >>
More C# Articles
More By Michael Youssef