Solved

Question on IL code

Posted on 2004-07-30
6
312 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Author Comment

by: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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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