tags:

views:

661

answers:

8

Newb question:

Does a Program that is written with the Microsoft .NET framework compile/execute native code?

I don't mean if there is a way not to have to install the .net framework on a machine. Simply put: does a .net application run on another layer like Java (i.e. bytecode).

+1  A: 

Short answer (to your second question): Yes. It's called the Common Language Runtime (CLR).

Brief overview of the framework is available at Wikipedia: http://en.wikipedia.org/wiki/.NET_Framework#Architecture

felideon
+1  A: 

When you compile a .NET program it is compiled into IL (Intermediate Language). IL is very similar to assembly language. That IL is then converted into native code at run time through the JIT (Just In Time) compiler.

So, to answer your question, yes and no. The compiler does not emit native code but rather IL which is converted to native code when the application is run.

Dan Rigby
+23  A: 
CMS
A picture is worth a thousand words
Pascal Paradis
+1 for rad diagram
Alex Baranosky
I always heard MSIL as opposed to CIL. Is calling it MSIL on the outs?
PhoenixRedeemer
@PhoenixRedeemer: Originally was known as MSIL, during the beta releases of the .NET languages, but due the standardization (Ecma-335 http://is.gd/gchW ) is now officially known as CIL...
CMS
A: 

Well, at least with mono I do know that you can generate native code instead of managed assemblies. I think there are OpenSource tools for Windows that are able to do this, too.

BeowulfOF
A: 

I think you have to be careful about asking whether it is "compiled" and assuming that this means that the result emitted must be "native code." This isn't what "compiled" means:

"We generally say that a language is 'interpreted' when the initial translator is simple. If the translator is complicated, we say that the language is "compiled." The distinction can be confusing because "simple" and "complicated" are subjective terms, and because it is possible for a compiler to produce code that is then executed by a complicated virtual machine (interpreter)...These two characteristics - thorough analysis and nontrivial transformation - are the hallmarks of compilation."

From "Programming Language Pragmatics" by Michael L. Scott. Oddly enough, this is a textbook on programming language theory that I just happened to be reading this afternoon with the page open to that quote.

So, yes, C# and other .NET languages run on the CLR and they are compiled.

Mark Brittingham
+6  A: 

Sort of. The .NET compiler compiles your source code into IL (an intermediate language) and packages it in an assembly (usually one .DLL or .EXE file) which you deploy. At run-time, it is hosted by a CLR (common language runtime) which is responsible for executing the code, enforcing security rules, and so on. The main desktop CLR for Windows (there are others like Mono and Silverlight) doesn't interpret the IL, but rather "JIT"s (just in-time compiles) the IL code into native code before executing methods (functions).

Note there are actually some performance advantages to just in-time compilation. For example, the CLR can optimize the native code it generates based on performance characteristics of the machine it is running on like type of CPU, CPU cache size, number of CPUs, RAM size, etc. Traditional compilers can't do this as they don't know what machine the code they generate will ultimately be executed on.

Also, assemblies can be "pre-JITted" using a tool called ngen. In this process the native code is compiled from the IL before the assembly is executed and cached on disk. That way, no JITting overhead is incurred at run-time.

C. Dragon 76
not many people knows about ngen
chakrit
A: 

C++ is the one .Net language which doesnt have to be compiled into IL, but you cant have ANY .NET references or code from .Net if you take this option.

Ther other thing to look into is what c dragon pointed out - ngen.exe which can have some remarkable improvments for many .Net windows/console based applications.

Ash
A: 

at the time of execution, compiler generates a IL code which is intermediate language code then the code is converted into native code by JIT compiler