Is C# partially interpreted or really compiled?

2019-01-21 07:41发布

There is a lot of contradicting information about this. While some say C# is compiled (as it is compiled into IL and then to native code when run), others say it's interpreted as it needs .NET. EN Wiki says:

Many interpreted languages are first compiled to some form of virtual machine code, which is then either interpreted or compiled at runtime to native code.

So I'm quite confused. Could anyone explain this clearly?

12条回答
祖国的老花朵
2楼-- · 2019-01-21 07:51

Too many semantics and statements based on opinion.

First off: C# isn't an interpreted language; the CLR and JVM are considered "runtimes" or "middleware", but the same name applies to things like Perl. This creates a lot of confusion among people concerned with names.

The term "Interpreter" referencing a runtime generally means existing code interprets some non-native code. There are two large paradigms: Parsing reads the raw source code and takes logical actions; bytecode execution first compiles the code to a non-native binary representation, which requires much fewer CPU cycles to interpret.

Java originally compiled to bytecode, then went through an interpreter; now, the JVM reads the bytecode and just-in-time compiles it to native code. CIL does the same: The CLR uses just-in-time compilation to native code.

Consider all the combinations of running source code, running bytecode, compiling to native, just-in-time compilation, running source code through a compiler to just-in-time native, and so forth. The semantics of whether a language is compiled or interpreted become meaningless.

As an example: many interpreted languages use just-in-time bytecode compilation. C# compiles to CIL, which JIT compiles to native; by contrast, Perl immediately compiles a script to a bytecode, and then runs this bytecode through an interpreter. You can only run a C# assembly in CIL bytecode format; you can only run a Perl script in raw source code format.

Just-in-time compilers also run a lot of external and internal instrumentation. The runtime tracks the execution of various functions, and then adjusts the code layout to optimize branches and code organization for its particular execution flow. That means JIT code can run faster than native-compiled code (like C++ typically is, or like C# run through IL2CPP), because the JIT adjusts its optimization strategy to the actual execution case of the code as it runs.

Welcome to the world of computer programming. We decided to make it extremely complicated, then attach non-descriptive names to everything. The purpose is to create flamewars over the definition of words which have no practical meaning.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-21 07:53

First off let's understand the definitions of interpreted and compiled.

"Compile" (when referring to code) means to translate code from one language to another. Typically from human readable source code into machine code that the target processer can... process.

"Interpret" (when referring to code) ALSO means to translate code from one language to another. But this time it's typically used to go from human readable source code into an intermediate code which is taken by a virtual machine which interprets it into machine code.

Just to be clear
Source code -> Compiler -> Machine code
Source code -> Compiler -> Byte Code -> Interpreter -> Machine code

Any language can, in theory, be interpreted or compiled. Typically Java is compiled into bytecode which is interpreted by the Java virtual machine into machine code. C# is typically interpreted into bytecode which is compiled by the CLR, the common language runtime, another virtual machine.

By and far the whole thing is a marketing gimmick. The term "interpreted" was added (or at least, increased in usage) to help showcase how neat just-in-time compiling was. But they could have just used "compiled". The distinction is more a study of the English language and business trends rather than anything of a technical nature.

查看更多
4楼-- · 2019-01-21 07:53

Since a computer can only execute binary code, any language will lead to the production of binary code at one point or another. The question is : does the language let you produce a program in binary code? If yes, then it is a compiled language : by definition "compiled" in "compiled language" refers to compilation into binary code, not transformation into some intermediary code. If the language lead to the production of such intermediary code for a program, it will need an additional software to perform the binary compilation from this code : it is then an interpreted language. Is a program "compiled" by C# directly executable on a machine without any other software at all installed on this machine? if no, then it is an interpreted language. For an interpreted language, it is an interpreter that will generate the underlying binary code, most of the time in a dynamic way since this mechanism is the basis of the flexibility of such languages. rem. : sometimes it does not look obvious because the interpreter is bundled into the OS

查看更多
做个烂人
5楼-- · 2019-01-21 07:54

I believe this is a pretty old topic.

From my point of view, interpreted code will go through an interpreter, line by line translate and execute at the same time. Like example javascript, it is an interpreted code, when a line of javascript ran into an error, the script will just break.

While compiled code, it will go through a compiler, translate all code to another form of code at once, without execute it first. The execution is in another context.

查看更多
SAY GOODBYE
6楼-- · 2019-01-21 07:58

Look here: http://msdn.microsoft.com/library/z1zx9t92

Source code written in C# is compiled into an intermediate language (IL) that conforms to the CLI specification.

(...)

When the C# program is executed, the assembly is loaded into the CLR, which might take various actions based on the information in the manifest. Then, if the security requirements are met, the CLR performs just in time (JIT) compilation to convert the IL code to native machine instructions.

查看更多
再贱就再见
7楼-- · 2019-01-21 07:58

C# is both interpreted and compiled in its lifetime. C# is compiled to a virtual language which is interpreted by a VM.

The confusion stems from the fuzzy concept of a "Compiled Language".

"Compiled Language" is a misnomer, in a sense, because compiled or interpreted is not a property of the language but of the runtime.

e.g. You could write a C interpreter but people usually call it a "Compiled Language", because C implementations compile to machine code, and the language was designed with compilation in mind.

查看更多
登录 后发表回答