Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why .NET code compiles to MSIL?

Tags:

.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

like image 273
Siddiqui Avatar asked Dec 18 '09 05:12

Siddiqui


1 Answers

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.

like image 90
Matthew Olenik Avatar answered Oct 01 '22 18:10

Matthew Olenik