Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compile .NET IL code to machine code?

I would like to distribute my .NET programs without the .NET framework. Is it possible to compile a .NET program to machine code?

like image 515
litter Avatar asked Sep 26 '08 17:09

litter


People also ask

Does .NET compile to machine code?

NET, programs are not compiled into executable files; they are compiled into Microsoft Intermediate Language (MSIL) files, which the CLR then executes. The MSIL (often shortened to IL) files that C# produces are identical to the IL files that other . NET languages produce; the platform is language-agnostic.

Is C# compiled to machine code?

As has been said, C# is compiled into IL (providing binary portability) then during execution the IL is compiled into machine code. Since it becomes machine language during execution, it is usually as efficient as C++.

How is C# converted to machine code?

The C# code is converted to MSIL, which is the assembly language that . NET's virtual machine understands. The MSIL is then compiled to machine code using Just-in-time compilation. The process is done not just when you press F5.


1 Answers

Yes, you can precompile using Ngen.exe, however this does not remove the CLR dependence.

You must still ship the IL assemblies as well, the only benefit of Ngen is that your application can start without invoking the JIT, so you get a real fast startup time.

According to CLR Via C#:

Also, assemblies precompiled using Ngen are usually slower than JIT'ed assemblies because the JIT compiler can optimize to the targets machine (32-bit? 64-bit? Special registers? etc), while NGEN will just produce a baseline compilation.

EDIT:

There is some debate on the above info from CLR Via C#, as some say that you are required to run Ngen on the target machine only as part of the install process.

like image 165
FlySwat Avatar answered Sep 20 '22 14:09

FlySwat