Solved

Question on IL code

Posted on 2004-07-30
6
310 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:knowlton
6 Comments
 
LVL 15

Expert Comment

by:Timbo87
Comment Utility
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.
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
they will NOT necesarily be the exact same but they will be very similar take a quick look here http://builder.com.com/5100-6373-1027686.html its a good example of how different they can be.
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
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 vb.net ... then disassemble them.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Author Comment

by:knowlton
Comment Utility
>>>> 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
{
      .entrypoint
      .maxstack      1


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

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

      ret

}




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.
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 500 total points
Comment Utility


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;

// THIS
i = 0;
for (int j=0;j<50;j++)
   i += j;
Console.Writeline(i);


//COMPARED TO THIS
i=0;
i+=1;
i+=2;
i+=3;
i+=4;
...
..
.
i+=49;
i+=50;
Console.Writeline(i);


Programming directly in IL (using OpCodes and Reflection Emit), is similar.  a lot more coding, but a lot less overhead.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now