tags:

views:

783

answers:

10

Hi,

Learning MSIL is fun and all that. Understanding what is going on "under the hood" can in many ways improve how you write your code performance-wise. However, the IL that is produced by the compiler is quite verbose and does not tell the whole story since JIT will optimize away a lot of the code.

I, personally, have had good use of my very basic IL understanding when I've had to make a small fix in an assembly I do not have the source code for. But, I could as well have used Reflector to generate C# code.

I would like to know if you've ever had good use of MSIL understanding and/or why you think it is worth learning it (except for the fun in it, of course). I'd also like to know if you think one should not learn it and why.

+3  A: 

You can dynamically create classes with Reflection.Emit, but you need to output the code in IL. Of course you could just compile the code you want and then view the IL in Reflector, but some level of IL knowledge is helpful.

Daniel Plaisted
+2  A: 

One Reason to learn MSIL is when you want to port your language to .net. Come on, who does NOT want a .net Version of COBOL? For Cobol.net to happen, you obviously need MSIL understanding to write the compiler.

Michael Stum
And yes, I know that Fujitsu offers a Cobol compiler for .net already, I just used Cobol as an example.http://www.netcobol.com/products/windows/netcobol.html
Michael Stum
Great answer! :)
mannu
A: 

I would have to say that its good to know some MSIL even just the basics, so that you can see whats going on under the hood mainly to see how things are being optimized. I can't really see any use in learning it to deeply, deep enough to write it by itself anyway.

Like Michael said its handy if you want to port a language to .net

Nathan W
+10  A: 

In this blog post, I was able to use MSIL to prove beyond a doubt that for(int i = 0; i < count; i++) and for(int i = 0; i < count; ++i) are identical (e.g. the latter has no performance advantage).

I don't think MSIL knowledge is essential, but it certainly helps to understand that the CLR is a stack based machine since others (e.g. JVM) are similar at their core.

Jeff Moser
Looks like a very nice blog post. I am reading it now. :)
mannu
A: 

If you ever get to the stage where you need to look at more micro-optimisations being able to read (not necessarily write) CIL is helpful. Other than that, I personally have had little call to learn it.

It's also important to remember that CIL isn't the end of it as well.

ICR
+3  A: 

Knowing MSIL was essential for me on a recent project that involved analyzing emitted bytecode and instrumenting it, ie modifying the instruction stream to add additional behavior. Certainly this was an unusual requirement, but if you need to do this, you're screwed if you don't have a deep knowledge of MSIL.

Understanding MSIL offers similar advantages to knowing C, or especially x86 assembler. You may not use it much but it improves your overall understanding and proficiency using the platform in subtle but tangible ways.

Vern Eastley
+5  A: 

I found it useful writing Lolcode.net.

Nick Johnson
A: 

I think it's generally useful to understand how the CLI works (which includes a bit CIL of course).

Currently i am working through the CLI specification, and it really helps to understand what's going on, how the runtime handles memory, metadata etc. and why some things work and others don't. It's more than 500 pages, but i think it's worth it.

Botz3000
+1  A: 

For a C# developer, knowing CIL will enable you to take advantage of some of the CLR features that C# does not give you access to. i.e. force non virtual calls, exception filters, enable tail calls (this I think C# does not emit, but not fully checked), implementing static methods on interfaces (yes I mean interfaces) and so on. These features are rarely ever required, but if you have them in the toolbox, you have the option ;-)

MaLio
+1  A: 

System.Reflection.Emit.DynamicMethod is an extremely useful class which lets you create temporary, garbage-collected(!), compiled implementations of otherwise-interpreted languages. Think of the compiled Regex implementation, and how it could be useful to create something like that for your own little language in a performance-critical path.

You need to emit IL (the easiest way is through the System.Reflection.Emit.ILGenerator class) to use it.

Doug McClean