Compilation And Runtime Execution Of A C-Sharp Program

Introduction

C-Sharp (C#) is a strongly typed object-oriented programming language designed to give optimum composition of simplicity, expressiveness, and performance.

Recently, Microsoft announced a new platform or framework called. NET. C# is one of the many languages based on the .NET framework. .NET framework provides a runtime environment called Common Language Runtime, also called CLR (similar to Java's JVM), a set of libraries that can be exploited by a wide variety of languages. Java is based on a framework called J2EE, which provides a set of libraries that can be used only by Java.

CLR manages the execution of code and provides services that make development easier. CLR provides services such as cross-language integration, cross-language exception handling, enhanced security, versioning and deployment support, a simplified model for component integration, and debugging and profiling services.

To enable the runtime to provide these services, language compilers are required to compile source code into an intermediate language code. All .NET languages that aspire to use the CLR services compile (commandprompt>csc <filename>. cs) to something called Portable Executable (PE). PE is a collection of Microsoft Intermediate Language Code (MSIL) and Metadata.

MSIL is a CPU-independent set of instructions, and Metadata is the data that describes to the runtime the types, members, and references in the code. MSIL includes instructions for loading, storing, initializing, and calling methods on objects as well as instructions for arithmetic and logical operations, control flow, and exception handling.  All components developed on .NET carry information (metadata) about the components and resources they were built against. The runtime uses this information to ensure that your component or application has the specified versions of everything it needs. Registration information and state data are no longer stored in the registry. The presence of Metadata along with MSIL makes the code describe itself, which means the developer doesn't need to bother about type libraries or IDL or registration of components.

java source

C# source

Now when this .exe is run (commandprompt><filename>[.exe]), the MSIL is compiled into CPU-specific code by a Just-In-Time compiler (JITer). The runtime provides one or more JIT compilers for each computer architecture that runtime operates on. This feature allows developers to write a set of MSIL that can be JIT compiled and executed on computers with different architectures. The CLR doesn't provide an interpreter to interpret the MSIL Code. Instead the MSIL Code will always run native by compiling the whole MSIL at one shot to the machine code of the underlying computer architecture. The JVM on the other hand can interpret the bytecode using a Java interpreter or can be compiled entirely to native code using a JITer.

This MSIL format paves way for one more feature called language independence. Code and objects written in one language can be compiled to MSIL format, once a compiler is developed for that language. This enables other languages like Cobol etc. to generate MSIL code for .NET runtime that can be used by modern languages like C#. Java offers language independence in completely a different way. Java relies on CORBA to use shared set of objects from multiple languages with an Object Request Broker (ORB).

This is what Anders Heilsberg, chief C# Language Architect said during an interview with Osborne Press.

We've taken an approach that says we want to be a multilingual platform. We're going to build a platform that actually allows you to implement multiple programming languages and also have them share a common set of API's."


Recommended Free Ebook
Similar Articles