Common Intermediate Language

What is the Common Intermediate Language in C#?

The Common Intermediate Language (CIL) is a fundamental programming language in the realm of .NET development software languages.

Özkan ARDİL
.Net Programming

--

What is Common Intermediate Language in C#

Behind every seamless C# application lies a hidden layer of power and complexity known as Common Intermediate Language (CIL).

It’s the language spoken by your code when it interacts with the .NET runtime, and understanding it is akin to deciphering the inner workings of a machine.

In this article;

  • We’ll peel back the layers of C# development to reveal the magic of CIL,
  • We’ll demystify the role of the Just-In-Time (JIT) Compiler,
  • Explore the intricate world of Intermediate Language Disassembler (ildasm.exe),
  • And equip you with the knowledge to peer under the hood of your C# programs.

So, let’s dive into it.

Common Intermediate Language (CIL)

When you write source code in languages compatible with the .NET ecosystem, such as C#, Visual Basic, or F#, the compilation process converts this source code into Common Intermediate Language.

When the application is executed, the Common Language Runtime’s Just-In-Time (JIT) Compiler comes into play, translating the CIL code into binary code.

The JIT Compiler is an integral feature of the Common Language Runtime (CLR) responsible for this real-time translation of Common Intermediate Language (CIL) code into binary code during the execution of the program.

Now, let’s illustrate this translation process by examining a simple C# code snippet and observing how it manifests in CIL.

To do that I created a console application named as CilOne. You can access the source code of the project on my C# Subjects GitHub repository.


Console.WriteLine("Hello World using C#!");
Console.ReadLine();

To inspect the CIL code, we need to ensure that the solution is built in Visual Studio. Afterward, we can employ the Intermediate Language Disassembler, known as Ildasm, to view the CIL code.

Ildasm: Intermediate Language Disassembler

Ildasm is a tool designed for this purpose, and it comes bundled with the installation of .NET on your machine.

You can use the Ildasm.exe (IL Disassembler) to view Microsoft intermediate language (MSIL) information in a file. If the file being examined is an assembly, this information can include the assembly’s attributes and references to other modules and assemblies.

You can typically find it in the following directory:

C:\Program Files (x86)\MicrosoftSDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\ildasm.exe. Let’s execute this tool:

Directory of the ildasm.exe

Next, we need to locate the .dll file for which we want to examine the CIL code.

An easy way to do this is by right-clicking on the project in Visual Studio and selecting “Open Folder in File Explorer.” From there, navigate to the output folder, which could be something like /bin/Debug/net6.0.

Inside this folder, you’ll find the .dll file that Visual Studio generated:

Project .dll file path

You can simply drag and drop this .dll file onto the Ildasm tool or click on the file > open menu and select the related DLL file of your .NET project.

The solution is seen within CIL as follows:

ildasm.exe project tree explorer

Now, let’s take a closer look at how the CIL code represents the Main method.

CIL Code

Double-click on the Main method in Ildasm, and you’ll see the corresponding CIL code:

CLI code of main method

Now, double-clicking the Manifest icon in the MSIL Disassembler window produces the following output:

CLI code of main manifest

This might not be the most beautiful programming language you’ve ever seen, but on the other hand, it is not completely unreadable, as one could expect.

The following list describes each directive in the assembly manifest of the Hello.exe assembly used in the example:

.assembly extern <assembly name>: Specifies another assembly that contains items referenced by the current module (in this example, mscorlib)

.publickeytoken <token>: Specifies the token of the actual key of the referenced assembly

.ver <version number>: Specifies the version number of the referenced assembly

.assembly <assembly name>: Specifies the assembly name

.hash algorithm <int32 value>: Specifies the hash algorithm used

.ver <version number>: Specifies the version number of the assembly

.module <file name>: Specifies the name of the modules that make up the assembly. In this example, the assembly consists of only one file

.subsystem <value>: Specifies the application environment required for the program. In this example, the value 3 indicates that this executable is run from a console

.corflags: Currently a reserved field in the metadata.

It’s important to remember that all .NET-compatible languages, not just C#, are compiled into CIL. This compilation process facilitates interoperability between different .NET languages, enabling, for instance, a C# class to inherit from an F# class because they both ultimately compile into the same programming language — the Common Intermediate Language (CIL).

Source Code

You can access the source code of the project on my C# Subjects GitHub repository.

If you can give the repository a star and share the article, you will support me in reaching more people.

👏 If you found the content useful, I would appreciate it if you could support it with applause (you can also contribute with more than one applause by holding down the clap button for a long time).

⬇️ Check out my other articles.

💬 Let me know in the comment section what have I missed? Where I was wrong?

👨‍👦‍👦 You can also share this article with your friend. Argue a little on it. It is known that the truth is born in a dispute.

🙃 Stay determined, stay focused, and keep going

Thanks for reading…

--

--

Özkan ARDİL
.Net Programming

.NET C# JS and Angular dev with 8+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.