tags:

views:

293

answers:

4

Hi All,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

+14  A: 

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.

Matt Olenik
Cross-platform doesn't really apply much to the .NET framework since it was only meant to run on Windows, but still a valid point. +1
musicfreak
Really? Then explain Mono. :)
Daniel Bruce
@Matt can you kindly explain how can I make it possible to run my .net code on diffrent platform? Means if i want to run my .net application on linux? Is it possible?
Arman
.NET was not meant to run only on Windows. v1 docs says already mention this. What happens is that Microsoft didn't bother to implement VMs for other platforms.
Martinho Fernandes
musicfreak - while MS.NET is Windows-only, it still has to consider translating to the APIs under 2000, XP, Vista, 7, and beyond, not to mention x86 vs x64, plus x128 (if 8 supports it), plus whatever's coming. "Windows" is far from homogeneous.
rwmnau
@Arman The Mono folks maintain a good guide for writing portable .NET code: http://www.mono-project.com/Guidelines:Application_Portability
ZoogieZork
x128, lol. And don't forget XNA and XBoX.
Martinho Fernandes
Silverlight is also to mention. Not strictly Cross-Platform, but it benefits from their approach. Also, Microsoft DID release a CLR for BSD, it just was never updated past Version 1.0 and is therefore useless nowadays.
Michael Stum
Thanks ZoogieZork.
Arman
@musicfreak Here: http://msdn.microsoft.com/en-us/library/ht8ecch6(VS.71).aspx
Martinho Fernandes
Plus .NET Compact Framework and Micro Framework.
Lex Li
On Singularity/Helios research OS, MSIL will be used to target GPUs ISA.
Stringer Bell
+5  A: 

The answer can be found at MSDN

pipelinecache
+1  A: 
  • 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).
envalid
You're right about the functional languages assertion. GPUs are really powerful for *certain* situations. Functional languages make it much easier for the infrastructure to decide when those situations arrive.
Martinho Fernandes
I think future will more likely be a mix of both, FP + massively thread parallel code (GPGPU).
Stringer Bell
+1  A: 

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.

Manoj