Question on IL code

I will check this at some point...but I'm wondering about IL code.

From this book I'm reading, I know that under .NET...all languages are compiled into IL code, and then the JIT turns them into native executable code.

So does this mean that if I write a program that writes "Hello World" out to the console in C#.......and do the same thing in VB .NET.....the IL code that each language outputs will be exactly the same?
Tom KnowltonWeb developerAsked:
Who is Participating?

If you get good at writing IL code you can use reflection emit to generate compiled classes on the fly.  You do 10 times more coding, but the application gotes 100 times faster.

I know, I know.... more code and it's faster?

try running the following code about 100,000 times and see which method is faster

int i;

i = 0;
for (int j=0;j<50;j++)
   i += j;


Programming directly in IL (using OpCodes and Reflection Emit), is similar.  a lot more coding, but a lot less overhead.
Yes, they both call the same function. I did this test a few months ago and the code was exactly the same (same method calls) except that VB added an attribute to Main that wasn't editable in the VS.NET.
they will NOT necesarily be the exact same but they will be very similar take a quick look here its a good example of how different they can be.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

the best way to learn this is to experiment for yourself... the SDK comes with a tool called ildasm.exe which will show the IL of a program ... play around with C# and ... then disassemble them.
Tom KnowltonWeb developerAuthor Commented:
>>>> the best way to learn this is to experiment for yourself

This may sound strange...but this is one of the hardest things for me to do.....taking the time to experiment!

The book did have a very short "Hello World IL style" example that I entered in and compiled using ilasm.exe:

//File                  :Hello.cil
//Author            :Tom Knowlton

//define some basic assembly information
.assembly hello {
      .ver 1:0:0:0

//create an entry point for the exe
.method public static void main( ) il managed
      .maxstack      1

      //load string to display
      ldstr "Hello world IL style\n"

      //display to console
      call void [mscorlib]System.Console::WriteLine( class System.String )



What was kinda strange was the example in the book had what I guess was a typo:

.assembly hello {

actually looks like this in the book:

.assembly hello 8: {

Not sure what the 8:    was supposed to be......but I took it out and then the code compiled and worked fine.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.