157

I'm new to .NET C# programming. I'm following few books. It is said that instead of compiling C# code directly to machine code, it is converted into an intermediate language (called MSIL aka CIL). But when I compile, I get an exe/dll file.

  1. Is this MSIL/CIL contained in these exe/dll files?
  2. I want to see that intermediate language code, just to get feel for its existence. How do I view it?
  3. They are calling this exe/dll file an assembly. Are they using this "fancy word" just to differentiate these from the exe/dll files that contain native/machine code?
Aaron Record
  • 330
  • 4
  • 13
  • http://ilgenerator.apphb.com/ https://github.com/max810/IlGenerator – juFo Jun 06 '19 at 08:05
  • it's called assembly because it's the instructions of the .NET VM. JIT-compiled programs run in a virtual machine such as JVM for Java, Dalvik VM for Android and CLI VM for .NET. Java bytecode is also called assembly. And now we have [web assembly](https://en.wikipedia.org/wiki/WebAssembly) which is the instructions of a VM. It's developed from the JS subset called [asm.js](https://en.wikipedia.org/wiki/Asm.js) – phuclv Mar 30 '21 at 16:09

13 Answers13

111
  1. Yes it is, more exactly in the .text section of the PE file (portable executable = *.exe or *.dll). More information can be found here.
  2. The best choice is to use ILSpy (Reflector is no longer free). It's a free disassembler that can dissassemble your assembly into MSIL but also C#, VB (to some extent). The .NET Framework SDK contains ILDasm, which is the official MSIL dissasembler.
  3. Basically yes. An assembly is a file that contains MSIL code and corresponding metadata. This is not restricted to PE files per se, but all current CLR implementations use them.

If I may recommend a good book on that matter too, it's Expert .NET 2.0 IL Assembler by Serge Lidin. He's the guy who designed MSIL.

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
  • Wait! It isn't located in `.text` sextion? If not, what does `.text` contain? and whats up with the `#` I thought sections are named beginning with `.`. So souldn't it be `.strings` –  Jul 24 '10 at 19:44
  • I'm curious, why is [ILDasm](https://learn.microsoft.com/en-us/dotnet/framework/tools/ildasm-exe-il-disassembler) not recommended? Is it only included with (some versions of?) Visual Studio? – Sinjai Jul 26 '17 at 14:32
  • 2
    Both have their places, but for most (> 99.99%) users ILSpy is the right choice. Its ability to decompile IL code back to C# ist invaluable for quickly navigating and reverse-engineering assemblies. If you're doing anything that emits raw IL (e.g. you're writing a compiler or rewriting assembly IL, e.g. with Mono.Cecil) then ILDasm comes in handy because it can show you the raw structure of IL, token tables etc. Again, most non-IL experts will be confused by those, so ILSpy is probably all you want and need. – Johannes Rudolph Jul 26 '17 at 15:15
  • 3
    Wow, swift response 7 years later! I haven't messed with IL much, or compared all the options (which is why I'm asking in the first place), but does LINQPad not have this decompiling feature? – Sinjai Jul 26 '17 at 15:20
  • I installed it in VS 2017, nothing happen when I click on see the code in ILSPY. – Anirudha Gupta Jun 19 '18 at 05:41
  • dotPeek is a nice alternative. – Ben Collins Apr 08 '19 at 12:42
  • 2
    Link to ILSpy is dead – BotMaster3000 Jan 03 '20 at 07:42
  • It's on GitHub https://github.com/icsharpcode/ILSpy – Michael Jun 30 '21 at 19:44
  • 1
    Is there any way to get ILSpy to actually show IL? – Nathan Chappell Dec 02 '21 at 08:46
53

One of my favorite ways to see IL for a snippet of C# is to use the free LINQPad tool. After entering some code and choosing "C# statements" at the top (or "C# Program", whichever suits), click the "IL" button under the code entry area, and you will see the generated IL.

Using LINQPad for this is much more convenient than loading up Visual Studio or running the compiler from the command line, then loading up ILDASM and opening the .il file with a text editor.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • Another good tool is the Snippy add-in for Reflector: http://jasonhaley.com/blog/post/2008/11/23/ReflectorSnippy-Addin.aspx – Thomas Levesque Jul 25 '10 at 00:00
  • Nice one! I up-voted but it is slightly more complicated if you have to include any custom classes. Still, I really like this one! – Andrew Steitz Aug 08 '18 at 15:33
35

If you want it online, .NET Fiddle is excellent. Just paste your code and click View IL option at the top right.

nawfal
  • 70,104
  • 56
  • 326
  • 368
25

Another option: Use ReSharper

enter image description here Source / IL synced view: left blue background line corresponds with right IL Block

In Visual Studio:

  • Choose ReSharper | Windows | IL Viewer
  • or Context Menu: Navigate | IL Code

Supports synced view of Source Code and IL - when you click on a statement in source, the corresponding block in IL is highlighted (and vice versa). Displays descriptions for IL from Microsoft Developer Network and "Common Intermediate Language (CIL) instruction set" from ECMA standard specification.

see Viewing Intermediate Language (IL) in Resharper Help. Picture above is from Resharper Help.

Free option is to use Jetbrains dotPeek

see also: "Exploring Intermediate Language (IL) with ReSharper and dotPeek", by Maarten Balliauw, January 19, 2017 - Jetbrains Blog

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Michael Brux
  • 4,256
  • 1
  • 20
  • 21
15

I believe that they are called "assemblies" because an assembly is a set of modules, assembled together by a manifest.

multi-file assembly
(source: microsoft.com)

See Assembly Contents for details.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
John Saunders
  • 160,644
  • 26
  • 247
  • 397
15

Sharplab sharplab is an online tool, great for simple use cases. Type your code on the left, IL shows up on the right.

Colin
  • 588
  • 6
  • 9
12
  1. Yes it is in assembly.
  2. You need .NET Reflector or ILDasm.
  3. More details on assembly check HERE.

P.S As you are following some books I will highly recommend you CLR via C#.

Incognito
  • 16,567
  • 9
  • 52
  • 74
8

In many respects, .NET assemblies are similar to Java bytecode packages.

  1. Yes. They also contain manifests and other data, but the CIL is part of the exe/dll.
  2. Use ILDasm or Reflector - most people would say Reflector, as it is much more powerful. Both will show you what CIL was produced. Wikipedia has a list of all CIL instructions, for a better feel (it is assembly like).
  3. I guess it is meant as an assembly of code. A good way to differentiate it from native.
Oded
  • 489,969
  • 99
  • 883
  • 1,009
7

I know this is an old question, and I'd prefer any of the tools above. However, in a pinch, there has been an MSIL viewer in the box with Visual Studio since at least Version 2005.

The tool is named ildasm.exe, and is located in the following folders after default Visual Studio installations:

Visual Studio 2005 "C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe"

Visual Studio 2008 "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe"

Visual Studio 2010 "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\ildasm.exe"

For more information, see: "How to: View Assembly Contents" in the MSDN Library.

Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76
7

I have just spent a couple of hours searching for the best tool that could let me view the IL code directly inside Visual Studio.

The only solution I found so far is Msiler https://visualstudiogallery.msdn.microsoft.com/60fc53d4-e414-461b-a27c-3d5d2a53f637

it works quite well!

Otherwise the second best solution, but without visual studio integration, is JetBrains dotPeek, which is quite awesome to be honest.

sebas
  • 1,045
  • 12
  • 27
2

I you want to view the intermediate language, Try downloading JustDecompile from Telerik (Which is currently free, requires a sign up though).

Drag in your DLL and choose IL from the drop down box at the top!

Luke
  • 22,826
  • 31
  • 110
  • 193
2

From my experience the best source of IL-related knowledge is Andrew Troelsen “Pro C# and .NET Platform”. Starting from 3rd edition he has really, really outstanding chapter (approx 50 pages) on how to understand IL and even write your own code and use ILAsm. I’ve employed that information to investigate whether multiple inheritance exists in .NET world. Also you could try to employ some very interesting features in IL (e.g. filtering of exceptions which only exists in VB but not in C#).

I highly recommend to read that chapter.

Eventually, .NET Reflector is an enterprise standard for investigating IL code of assemblies and Richter's book is definitely "must read" stuff. But from other books like mentioned above you could reveal really useful things :)

Yes, each assembly in .NET world holds some IL code (alongsite with manifest) which could be viewed thru Reflector or ILDasm. Even more, Reflector could show you C# and VB optimized code. This means that any person could view the source code of an assembly and that's why in commercial products obfuscators are used.

xenn_33
  • 855
  • 1
  • 10
  • 25
1

There is now another option. You can do this in VS Code with this extension built with Roslyn. This is currently limited to .NET Core.

Adam Caviness
  • 3,424
  • 33
  • 37