How can I view MSIL / CIL generated by C compiler? Why is it called assembly?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Viewing the Microsoft Intermediate Language (MSIL), also known as the Common Intermediate Language (CIL), generated by the C# compiler can provide valuable insights into how .NET applications function under the hood. This intermediate language is a low-level, human-readable code that is independent of any particular CPU architecture. It is a crucial part of .NET's execution model, converting high-level C# code into something more abstract before it is just-in-time (JIT) compiled into native machine code. Understanding how to view and analyze this code can be beneficial for debugging, learning, and optimizing your applications.
Why Is It Called Assembly?
In .NET terminology, an "assembly" is a compiled code library used for deployment, versioning, and security. It typically consists of IL code and metadata, encapsulating the essential details and resources of your application or library. The term "assembly" stems from the idea of piecing together a complete, deployable .NET application or library, including both code and encoded metadata, much like assembling components into a machine. Assemblies come in two primary forms:
- DLLs (Dynamic Link Libraries): Used for additional code libraries.
- EXEs (Executable files): Used to create stand-alone applications.
How to View MSIL/CIL
Tools to Analyze MSIL/CIL
Several tools can help you view and analyze the IL generated by the C# compiler:
- ILDasm (IL Disassembler):
- A tool provided by Microsoft as part of the Visual Studio or Windows SDK.
- It allows you to disassemble .NET assemblies and view their IL code.
- dotPeek:
- A free .NET decompiler from JetBrains that provides a user-friendly interface for examining the IL code.
- ILSpy:
- An open-source .NET assembly browser and decompiler that lets you explore the assembly content and corresponding IL code.
- Visual Studio:
- Has built-in support for viewing IL code, especially when debugging or using the intermediate view options.
Steps to View MSIL Using ILDasm
- Compile the C# Code:
- Ensure you have a C# file ready. For example, let's consider a simple C# program:
- Compile this code using the C# compiler (csc):
- Open the Developer Command Prompt or Windows Command Prompt.
- Run ILDasm with your compiled `.exe` or `.dll` file:
- This will open a graphical window with a tree view of the assembly's structure.
- Double click on the methods or classes to view their IL code. For example, for the `Main` method, you might see something like:
- `ldstr "Hello, IL!"`: Load the string "Hello, IL!" onto the evaluation stack.
- `call void [mscorlib]System.Console::WriteLine(string)`: Call the `WriteLine` method from `System.Console`.
- `ret`: Return from the method.
- Security and Performance: An understanding of IL can help in analyzing performance bottlenecks and security issues, especially when dealing with dynamically loaded modules or unsafe code.
- Debugging and Learning: Viewing IL is an excellent way for developers to learn more about how high-level C# constructs map to lower-level operations, which can enhance debugging skills and aid in education.
- Cross-Platform and Compatibility: Since IL is CPU-agnostic, the same assembly can be run on different platforms using appropriate .NET runtimes, promoting cross-platform compatibility.

