34

Sometimes I would like to quickly see the IL representation of my code snippets in C#, to understand what exactly happens to various code statements under the hood, like it's done here for example.

I know there are ildasm, Reflector, ILSpy, dotPeek and probably quite some other tools available. What I'm wondering here is that if there is any more elegant way from writing some lines of code to seeing the corresponding IL, than compiling your .net code, loading the assembly into one of those programs and finding the code you are curious about.

Maybe having a plugin for visual studio, which would add a right click option for "build and see IL code" or any other convenient way?

Edit:

After some more googling I found NDasm codeplex project which does what I was looking for - integrates into visual studio.

But vcsjones' LINQPad suggestion is great as well, thus I am marking it as accepted, thanks. A really useful tool ...

doblak
  • 3,036
  • 1
  • 27
  • 22

2 Answers2

52

I typically use LINQPad for that. Just paste in some C# and switch to the IL view. For example:

enter image description here

I like it because you can also just post expressions or statements instead of a full program:

enter image description here

Best of all, it's free (with optional premium features).

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • 8
    It is probably worth noting that LINQPad is an amazing tool that I think every developer should have, even if they never look at IL. It is helpful in *many* ways. – vcsjones Jan 27 '12 at 16:19
  • The IL also has tool tips, so you can see what it all does, if you don't know/recognise it. It will also comment in the variable names, as well as show their type on a tool tip. For example: hovering over ldc.i4.3 will give you: "Pushes the integer value of 3 onto the evaluation stack as an int32". I'm not aware which version support this, but it's in the latest version (V5.10) but I reckon it's also supported in the previous version (V4) – Enthusiastic Programmer Mar 26 '17 at 15:29
0

If you have Reflector, you can use the Snippy addin. Here is a blog post on it: Reflector Snippy Addin.

Jason Haley
  • 3,770
  • 18
  • 22