top of page
Writer's pictureThe Tech Platform

What is CLR and Its Execution in C#



CLR (Common Language Runtime)

The Common Language Runtime (CLR) is programming that manages the execution of programs written in any of several supported languages, allowing them to share common object-oriented classes written in any of the languages. It is a part of Microsoft's .NET Framework. The CLR is somewhat comparable to the Java virtual machine that Sun Microsystems provides for running programs compiled from the Java language. Microsoft refers to its CLR as a "managed execution environment." A program compiled for the CLR does not need a language-specific execution environment and can easily be moved to and run on any system with Windows 2000 or Windows XP.


CLR transforms source code into a form of bytecode known as Common Intermediate Language (CIL). At run time, CLR handles the execution of the CIL code.


Following are the functions of the CLR.

  • It converts the program into native code.

  • Handles Exceptions

  • Provides type-safety

  • Memory management

  • Provides security

  • Improved performance

  • Language independent

  • Platform independent

  • Garbage collection

  • Provides language features such as inheritance, interfaces, and overloading for object-oriented programmings.



Components in CLR



Source Code:

Source code is the fundamental component of a computer program that is created by a programmer. It can be read and easily understood by a human being. When a programmer types a sequence of C programming language statements into Windows Notepad, for example, and saves the sequence as a text file, the text file is said to contain the source code.


Source code and object code are sometimes referred to as the "before" and "after" versions of a compiled computer program.


Language Compiler:

It transforms source code written in one programming language into another programming language. ... When you run the C# compiler, it takes your code as an input, does some processing, and then outputs your program in intermediate language (IL) code which is saved in *.exe or *. dll files.


MSIL Code:

Instructions that are platform independent and are generated by the language-specific compiler from the source code. The MSIL is platform independent and consequently, it can be executed on any of the Common Language Infrastructure supported environments such as the Windows .NET runtime.


The MSIL is converted into a particular computer environment specific machine code by the JIT compiler. This is done before the MSIL can be executed. Also, the MSIL is converted into the machine code on a requirement basis i.e. the JIT compiler compiles the MSIL as required rather than the whole of it.


Metadata:

The data providing information about one or more aspects of the data; it is used to summarize basic information about data which can make tracking and working with specific data easier. Some examples include: Means of creation of the data. Purpose of the data. Time and date of creation.


Native Code:

Programming code that is configured to run on a specific processor. Native code will generally not function if used on a processor other than the one it was specifically written for unless it is allowed to run over an emulator.


Common Language Specification (CLS): It is responsible for converting the different .NET programming language syntactical rules and regulations into CLR understandable format. Basically, it provides the Language Interoperability. Language Interoperability means to provide the execution support to other programming languages also in .NET framework.


Language Interoperability can be achieved in two ways :

  1. Managed Code: The MSIL code which is managed by the CLR is known as the Managed Code. For managed code CLR provides three .NET facilities:

  2. Unmanaged Code: Before .NET development the programming language like .COM Components & Win32 API do not generate the MSIL code. So these are not managed by CLR rather managed by Operating System.


Common Type System (CTS): Every programming language has its own data type system, so CTS is responsible for understanding all the data type systems of .NET programming languages and converting them into CLR understandable format which will be a common format.


There are 2 Types of CTS that every .NET programming language have :

  1. Value Types: Value Types will store the value directly into the memory location. These types work with stack mechanism only. CLR allows memory for these at Compile Time.

  2. Reference Types: Reference Types will contain a memory address of value because the reference types won’t store the variable value directly in memory. These types work with Heap mechanism. CLR allots memory for these at Runtime.


Garbage Collector: It is used to provide the Automatic Memory Management feature. If there was no garbage collector, programmers would have to write the memory management codes which will be a kind of overhead on programmers.


JIT (Just In Time Compiler): It is responsible for converting the CIL(Common Intermediate Language) into machine code or native code using the Common Language Runtime environment.



Features of CLR:

  1. .NET Framework Class Library support: Contains built-in types and libraries to manage assemblies, memory, security, threading, and other runtime system support

  2. Debugging: Facilities for making it easier to debug code.

  3. Exception management: Allows you to write code to create and handle exceptions.

  4. Execution management: Manages the execution of code

  5. Garbage collection: Automatic memory management and garbage collection

  6. Interop: Backward-compatibility with COM and Win32 code.

  7. Just-In-Time (JIT) compilation: An efficiency feature for ensuring that the CLR only compiles code just before it executes

  8. Security: Traditional role-based security support, in addition to Code Access Security (CAS)

  9. Thread management: Allows you to run multiple threads of execution

  10. Type loading: Finds and loads assemblies and types

  11. Type safety: Ensures references match compatible types, which is very useful for reliable and secure code



Benefits of CLR:

  • It improves the performance by providing a rich interact between programs at run time.

  • Enhance portability by removing the need of recompiling a program on any operating system that supports it.

  • Security also increases as it analyzes the MSIL instructions whether they are safe or unsafe. Also, the use of delegates in place of function pointers enhance the type safety and security.

  • Support automatic memory management with the help of Garbage Collector.

  • Provides cross-language integration because CTS inside CLR provides a common standard that activates the different languages to extend and share each other’s libraries.

  • Provides support to use the components that developed in other .NET programming languages.

  • Provide language, platform, and architecture independence.

  • It allows easy creation of scalable and multithreaded applications, as the developer has no need to think about the memory management and security issues.



The Tech Platform

0 comments

Comments


bottom of page