Solved

Question on IL code

Posted on 2004-07-30
6
316 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 11679752
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
ID: 11680633
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
ID: 11680646
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 11680688
>>>> 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
ID: 11680733
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 500 total points
ID: 11680949


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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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