Is C# partially interpreted or really compiled?

后端 未结 13 1099
别跟我提以往
别跟我提以往 2020-12-07 11:51

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 interpre

相关标签:
13条回答
  • 2020-12-07 12:41

    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.

    0 讨论(0)
  • 2020-12-07 12:44

    C# is compilable language.

    Probably, as I met too those kind of opinions, the fact that someone thinks that there is an Interpreter for C# language, is due the kind of projects like

    C# Interpreter Console

    or, for example, famous

    LinqPAD

    where you can write just lines of the code and execute them, which brings to think that it's Python like language, which is not true. It compiles those lines and executes them, like a ordinary compilable programming language (from workflow point of view).

    0 讨论(0)
  • 2020-12-07 12:46

    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.

    0 讨论(0)
  • 2020-12-07 12:51

    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.

    0 讨论(0)
  • 2020-12-07 12:52

    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.

    0 讨论(0)
  • 2020-12-07 12:56

    C# is compiled into IL, by the c# compiler.

    This IL is then compiled just-in-time (JIT) as it's needed, into the native assembly language of the host machine. It would be possible to write a .NET runtime that interpreted the IL instead though. Even if this was done, I'd still argue that c# is a compiled language.

    0 讨论(0)
提交回复
热议问题