I would like to distribute my .NET programs without the .NET framework. Is it possible to compile a .NET program 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.
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++.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With