What is an IL code, CLR, CTS, CLS, JIT, Managed and Unmanaged code

 

Introduction

Hey Friends..! It's been long time since writing articles on @onlinebuff. Due to some health issues and working outside town not getting time to write more articles on @onlinebuff. Finally I'm back to home town with good health and from now on will write article regularly. Thanks for visting my blog and reading articles. Hope you have found my blog posts useful and helpful. If anything missing kindly send us your feedback your valuable feedback will help us to grow.

This article which speaks about basic but important topics related to .NET. These topics are frequently asked in interviews especially to freshers . Freshers who are looking to seek their job in DotNet for them this article in useful. In this post we will discuss about CLR (Common Language Runtime), CLS (Common Language Specification) , CTS (Common Type System), JIT (Just in Time Compiler), IL (microsoft intermediate language), Managed Code and Unmanaged Code.

Above the image shows how dotnet compiles code.

There are different languages on dotnet platform like VC#, VB.NET, J#,, VC++ and so on and each of these languages on DOTNET platform have their own compilers like for example VC# language have cse.exe compiler.

When we write any c-sharp code then cse.exe compiler comes into picture and compiles the code into half-partially compiled code which is called as MSIL code or IL code. Further for full compilation of code CLR comes and takes that half compiled code and does type checking, code verification and pass it to JIT (Just in time compiler). JIT takes that half partially compiled code and as per your machine/server environment it compiles the code into full compilation. This is how dotnet compiles the code. In the coming article we will talk about asp.net page-life cycle.

What is an IL Code?

IL code also known as Common intermediate language code or Microsoft intermediate language code.

IL code is a CPU independent partially half compiled code.

.Net platform has got many different programming languages like C#, VB.NET, J#, VC++ and etal. These languages have their own respective compilers, for example C# language has got cse.exe compiler and VB.NET has got vbc.exe compiler. So when we write our code in any specific language like (for eg C# language) and compiles the code, respective compiler (i.e. cse.exe compiler) take the action and compiles the c-sharp code into partially half compiled code. This half partially compiled code is known as IL code.

Why IL code is half compiled code?

IL code is a half partially compiled because it enables the platform and CPU independent feature of the .NET framework, by allowing these half compiled source code to be executed in any environment supporting the .NET specification. In other words it is independent of CPU configuration, machine configuration, Operating system, security config.So the IL code is half compiled code and on runtime this code is compiled to a machine specific code using the environmental properties (i.e. CPU configuration, machine configuration, Operating system, security config etc).

Example of IL Code with source code

Let's demonstrate IL code example. Let's first create a simple windows-application in C# and a simple button. In that button event add a simple basic code to display hello as shown in below source code.

 

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
        }
  
        private void button1_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show("Hello");
        }

}

Now compile this above code and open visual studio command prompt. Inorder to open visual studio command prompt go the program files -> Microsoft 2010 -> Visual Studio Tools -> Find Visual Studio Command Prompt.

A command prompt will open. Inorder to open ILDASM tool type "ildasm" and hit enter button.

Click on file -> Open and select exe file or DLL file of above windows-application.

Select/Double Click button click event as displayed on ILDASM tool a editor will open which will display ILCODE as shown in below image.

What is CLR - Common Language Runtime ?

The Common Language Runtime is the execution engine for .NET Framework applications.

CLR (Common Language Runtime) is a heart of Dot Net Framework. The core function of dotnet framework to execute application and to convert Managed code to native code to read more on roles of CLR please check this article.

It provides a number of services, including the following:

1. Code management (loading and execution)

2. Application memory isolation )

3. Verification of type safety

4. Conversion of IL to native code using JIT

5. Access to metadata (enhanced type information)

6. Managing memory for managed objects (garbage collection).

7. Enforcement of code access security (CAS)

8. Exception handling, including cross-language exceptions

9. Interoperation between managed code, COM objects, and pre-existing DLLs (unmanaged code and data)

10. Automation of object layout

11. Support for developer services (profiling, debugging).

What is CTS (Common Type System) ?

Common Type System allows two different languages of dotnet (C#, VB.NET) will get compiled into common base type for smooth communication.CTS is handled by the CLR.

Now we will check wheather two different languages data-types get compiled into common base type or not. Inorder to check that first let's write code example of c-sharp and then code example of vb.net.

Example of Common Type System

C# Code</p>

 

 public class Class1
 {
       
        public int Add()
        {

            int i = 0;
            i = i + 1;

            return i;
        }

 }

VB.NET code

 

Public Class Class1
    Public Function Add() As Integer
        Dim i As Integer
        i = i + 1
        Return i
    End Function
End Class


As you see we have created two examples in two different languages. Both of these examples having sample function that is Add() with a integer data-type. But in c# interger data-type is written as "int" and vb.net data-type written as "integer". Now let's check wheather both data-types will inherit from common-base type or not. Inorder to check that let's open ILDASM tool and check both code so open two ILDASM tool and in one side open c# code and on other side open vb.net code.

Double click on Add() function from both ILDASM windows.

It is proved that both the function data-type inherit from common base type code i.e. int32 (System Integer Data-Type for all languages on dotnet).

What is CLS (Common Language Specification) ?

CLS is subset of CTS .CLS use to communicate Objects written in different .NET languages.

Common Language Specification (CLS) defines the rules and standards to which languages on dotnet platform must adhere to in order to be compatible with other .NET languages. Because of CLS in CLR it enables C# developers to inherit from classes defined in VB.NET or other .NET compatible languages. This is one big advantage working with CLR environment and dotnet languages.

Example of Common Language Specification (CLS)

As we know that vb.net is not a case sensitive while c# is a case sensitive. Let's do that example and violate rules.

 

    public class clsCsharpClass
    {
        public int varb;
        public int varB;
    }

This above have two variables with same but only case differen of letter "B". So let's try to consume this code in VB.NET.

 

Imports CLSViolation

Public Class Class1

    Public Sub Checker()
        Dim objVBCode As New clsCsharpClass
       
    End Sub


End Class

By using "objVBCode" object if we try to access both variables then it wont display because we have violated rules of vb.net. If change the variable names then it will display. (Note : vb.net is not a case sensitive ).

 

    public class clsCsharpClass
    {
        public int pqr;
        public int varB;
    }

Now you guys must be thinking how can we remember rules while coding because there are many different languages and to remember all the rules it is not easy. So if you want that dotnet automatically gives you an error while coding your language say c# then in the assembly file of c# you need to add the following code as shown below.

 
[assembly: CLSCompliant(true)]

The above code to be added in the assembly file in order to ensure that your code is CLS-compliant and can be consumed by any language without any issue.

What is Managed Code ?

Code which is runs on CLR environment is called as managed code.

What is UnManaged Code ?

Code which does not runs on CLR environment is called as unmanaged code.

What is JIT ?

Just-in-time compiler on CLR takes the half partially compiled code and compile into machine specific on demand at application run time.

Types of JIT

Pre-JIT Compiler (Compiles entire code into native code completely).

Econo JIT Compiler (Compiles code part by part freeing when required).

Normal JIT Compiler (It compiles only those methods that are called at runtime).

This is just brief to understand about JIT in the further upcoming blog post will cover complete details on JIT.

Hope you guys have enjoyed reading this article if any doubts kindly mail me or drop your comment in the comment section and don't forget to give thumbs-up in the social media networks (FB, G+, Twitter).

 

 

:: SHARE THIS POST ::

        
 

More DOT.NET Articles To Read Next:

 
Jul
01
2014

Step by Step to use Stored Procedures in Asp.Net C# with example

CLICK TO READ FULL ARTICLE

Views - 43259 |Category - DOT.NET

May
22
2014

Step by step 3 tier architecture in asp.net using c# example

CLICK TO READ FULL ARTICLE

Views - 159515 |Category - DOT.NET

Sep
17
2013

Step By Step Select, Insert, Update and Delete using ASP.NET C# and ADO.NET

CLICK TO READ FULL ARTICLE

Views - 232128 |Category - DOT.NET

Apr
18
2013

Roles of Common Language Runtime in Dot.Net Framework

CLICK TO READ FULL ARTICLE

Views - 33895 |Category - DOT.NET

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

8s5bu

 

 
Comments

Posted by Mallikarjunarao on 2015-08-15

Write differences of collectons and generic collections