English 中文(简体)
Why .NET code compiles to MSIL?
原标题:
  • 时间:2009-12-18 05:06:39
  •  标签:
  • .net
  • cil

First .NET code compiles to MSIL and then JIT convert it to machine dependent code. Can any one tell me what all benifits get because of the 2 step compilation.Thanks

最佳回答

There are a few reasons. First and foremost is probably to make it cross platform. If C# or other .NET languages compiled directly to native code, they would have to be recompiled for each platform they run on. With a VM, all code can be kept in an intermediate format, and you only need write a VM implementation for each platform.

Also, by having a language-agnostic intermediary language, you can have many high level languages (C#, VB.NET, Python etc) all referencing assemblies written in other languages. Since they all compile into the same thing, they can work seamlessly with each other.

There are also performance benefits. The JIT compiler can do aggressive optimizations specifically for the machine the code is running on at that time. I do not know how much optimization the .NET JIT compiler does in this sense, but there are very large theoretical benefits that could be had.

问题回答

The answer can be found at MSDN

First Conversion from High Level Language and then to Machine Level, this is how .Net platform is designed. The first layer take care of High Level language to MSIL and second level can concentrate on hitch & glitch of platform to convert from MSIL to machine level code. It mainly supports Language interoperability and may be in near future it will also provide Cross Platform support when Project like Mono will gain more ground.

  • An executable is not bound to the platform. For instance XNA targets both PPC (Xbox360) and x86 processors. Some programs will run on Mono on linux or OSX.

  • It allows you to better optimize for the target machine or replace missing functions:

    • For instance OSX >= 10.5 compiles in missing GPU instructions at runtime with OpenCL.
    • Lets say you are working on a CPU without floating point support, then you could emulate it with the JIT without needing a complete code rewrite.
    • At some point in the future it could be possible to offload processing into the GPU or other targets dynamically (I suspect functional languages are somewhat better suited for this).




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签