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
Solved

Question on IL code

Posted on 2004-07-30
6
313 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 38
SqlDependency to get update from sql to my c# app 2 61
I need help with Crystal Report's Parameter Prompt Form 2 47
Please explain purpose of GZIP 4 34
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

808 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