.netvisual-studio

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?


Solution

  • 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.