Is C# partially interpreted or really compiled?
Asked Answered
A

15

91

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?

Acima answered 12/1, 2012 at 15:11 Comment(4)
possible duplicate of Is C# Interpreted or Compiled?Propulsion
Related post - If C# is not interpreted, then why is a VM needed?Macnamara
inb4 someone invents a language that distributes as LLVM IR and requires users to install some standalone instance of LLVM.Hectic
I know this is old, but I think it's still relevant. Most discussion here comes from different definitions of "interpreted language" vs "compiled language". The question might be improved by (1) clarifying the OP's specific definition of these terms or (2) separating out the question of "interpret" vs "compile" from the question about what is available/standard for building/running C# code. For those interested in progress toward native ahead-of-time (Native AOT) compilation of C# .NET, see this repo.Haemophiliac
H
92

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.

Hadst answered 12/1, 2012 at 15:13 Comment(10)
But why is it then considered to be intepreted language by many programmers?Acima
Personally, I've never heard anyone call C# an interpreted language.Appear
Perhaps they are just mistaken, or mis-informed. Or maybe I'm wrong :-)Hadst
JIT compilation != interprettedCaptious
Why would you argue that c# is a compiled language? Could you elaborate a bit more on that?Hauler
@JoSmo csc.exe is the command line tool that builds c#. It stands for C-Sharp CompilerHadst
In C++, a knowledgable programmer can do things like choose templates over virtual methods to affect the efficiency of the produced code. In C# this is not really a thing. There are too many layers between the programmer and the not-really-compiled code. What's more, there is no need to ship a VM runtime with C++ code. With C# you still need an interpreter of some kind. Its not really compiled.Delila
@ErikAronesty You don't need an interpreter - you do need a jit compiler though.Hadst
If processes, like garbage collection, remain in control of the JIT engine as the program is run.... it's an interpreter. If you need to ship a program to run your program... it's interpreted. Relabeling things based on the efficiency of interpretation is silly.Delila
@ErikAronesty That is still not what would make C# an interpreted language. When you run a C# code, the byte code is compiled into native code and cached by the Common Language Runtime (CLR).Acima
T
49

A purely compiled language has some advantages. Speed, as a rule, and often working set size. A purely interpreted language has some advantages. Flexibility of not needing an explicit compilation stage that allows us to edit in place, and often easier portability.

A jitted language fits in a middle ground in this case.

That's a reason alone why we might think of a jitted language as either compiled or as interpreted depending on which position on which metric we care about attaining, and our prejudices for and against one or the other.

C# can also be compiled on first run, as happens in ASP.NET, which makes it close to interpreted in that case (though it's still compiled to IL and then jitted in this case). Certainly, it has pretty much all the advantages of interpreted in this case (compare with VBScript or JScript used in classic ASP), along with much of the advantages of compiled.

Strictly, no language is jitted, interpretted or compiled qua language. We can NGen C# to native code (though if it does something like dynamically loading an assembly it will still use IL and jitting). We could write an intepretter for C or C++ (several people have done so). In its most common use case though, C# is compiled to IL which is then jitted, which is not quite the classic definition of interpreted nor of compiled.

Trace answered 12/1, 2012 at 15:42 Comment(2)
I agree with no language is jitted, interpretted or compiled qua language. I was seeing a bit about python which first gets compiled to byteCode and then at run time that byteCode gets interpreted by the interpretor of the respective OS. This fact only added to my amazement caused by the definitions of compiled, jitted and interpreted langauges.Macnamara
JIT languages can be faster than native. You know that instrumentation thing gcc has, where it outputs a performance measurement file on run and then feeds that back into gcc to compile a more-optimized binary? JVM and CLR both do that continuously.Katmandu
K
21

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.

Katmandu answered 17/1, 2016 at 3:5 Comment(6)
JVM interprets the bytecode and then compiles some of the methods/loop bodies to native code. This compilation can take place in parallel with the bytecode interpretation and the code can be hot swapped.Individualism
I can't find a cite for that. My understanding is that the JVM interprets the bytecode as it runs by JIT—that is: when it encounters bytecode it hasn't dealt with yet during the current run, it runs it through the JIT and executes the resultant native code block. JIT-and-execute is cheaper than bytecode interpretation, and implementing both systems is highly-complex.Katmandu
Checkout e.g. slideshare.net/ZeroTurnaround/…. What you are describing is what .NET does. What I described is what e.g. Hotspot JVM does and yes it's quite complex, that's why it's been developed for a while. You need to bytecode interpret the code to get profile information (think: which if branch was taken most of the time, etc.), which is than used in the optimizing compiler (this if branch was never taken, don't spend too much resources on optimizing it).Individualism
Actually perl is jitted too now.Delila
You can instrument profiling into native code. gcc has profiling tools as such. The compiler can identify a loop iterator and add code to compare its entry and exit states; it can set a variable when a branch is taken; and so forth. This can go off to total the number of calls, loop count, branch counts, and so forth between each time the JIT examines the profiling data. It costs a few cycles—not nearly as much as interpreting bytecode.Katmandu
The JVM doesn't always JIT-compile bytecode to native code. The most-used JVM called HotSpot - #16568753 - has strategies that decide whether to incur compilation overhead, or whether to interpret. The strategy is fairly simple in concept, it interprets initially, but JIT-compiles if a method is run multiple times. It can count method calls because the JVM is already incurring overhead tracking those (for stracktrace information etc). TL;DR: in Java, your run-once initialisation code is probably being interpreted.Berkie
S
18

If you feel, learned, or are old school, that a compiled EXE is going from source to machine code then C# is interpreted. If you think compiled means converting source code into other code such as byte code, then yes it’s compiled. For me, anything that takes run-time processing to work in the OS it was built for is interpreted.

Sickener answered 27/6, 2014 at 21:6 Comment(0)
F
12

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.

Fell answered 12/1, 2012 at 15:18 Comment(2)
Thanks, so if I undernstand correctly, the confusing with intepretes might come from virtual machine (CRM) that is needed, however it is not true intepreter.Acima
The link is already down ;-)Tangleberry
N
9

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.

Narcoanalysis answered 20/11, 2012 at 20:21 Comment(3)
In an interpreter, no machine code is generated at runtime, only existing machine code is executed. By that standard, it is completely incorrect to refer to the Common Language Runtime as an interpreter.Brahmanism
considering python and c#, i can just create modules and code in python and use it within the app without stopping it and compiling again, while in c# i have to stop the application to load the new code in. this for me makes the biggest difference specially considering Ai if its supposed to grow itself.Puritanism
Best of luck running at speed with that.Narcoanalysis
K
5

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.

Kobe answered 20/1, 2013 at 3:41 Comment(2)
No, the desktop .NET VM (called the Common Language Runtime) has a JIT compiler, no interpreter. In contrast, the .NET Microframework, found on tiny embedded devices which may not even support runtime code modification, actually does interpret the IL.Brahmanism
Source -> (compiler) -> Intermediate Language -> (interpreter) -> Native code. I'm viewing JIT as an interpreter optimization. It's basically "caching" at the interpreter layer. You can call both steps compilers if you like.Kobe
S
3

Most languages, if not all, requires an interpreter that translates their scripts to machine codes in order to allow the cpu to understand and execute it!

Each language handles the translation process differently!

For example, "AutoIt" is what we can describe as being a 100% interpreted language!

why?

Because "AutoIt" interpreter is constantly needed while its script is being executed! See example below:

Loop, 1000
Any-Code

"AutoIt" interpreter would have to translate "Any-Code" 1000 times to machine code, which automatically makes "AutoIt" a slow language!

In the other hand, C# handles the translation process differently, C#'s interpreter is required only once, before script execution, after that it is not required anymore during script execution!

C#'s interpreter would have to translate "Any-Code" only once to machine code, which automatically makes "C#" a fast language!

So basically,

  • A language that requires its interpreter during script execution is an "Interpreted Language"!

  • A language that requires its interpreter only once (before script execution) is a "Compiled Language"!

Finally,

  • "AutoIt" is an "Interpreted Language"!

  • "C#" is a "Compiled Language"!

Swash answered 24/1, 2019 at 0:53 Comment(0)
D
2

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.

Daniels answered 13/10, 2017 at 3:42 Comment(1)
JavaScript is either JIT-compiled immediately, like C#, or executed the same as HotSpot JVM, interpreted until parts of the code go hot, at which point it get JIT-compiled through an optimizing compiler. It depends on the JavaScript engine. JavaScript hasn't been interpreted for more than 10 years.Palatial
T
1

If we agree with the definition of interpreter «In computer science, an interpreter is a computer program that directly executes, i.e. performs, instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program.» there is no doubt: C# is not an interpreted language.

Interpreter on Wikipedia

Tricornered answered 7/11, 2017 at 8:59 Comment(0)
T
0

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

Thrum answered 12/1, 2012 at 15:25 Comment(2)
thank you. But still, C++ and C# both would be compiled languages from that point of view. But I think it not that clear as with C++ its directly compiled to native code while with .NET its just CIL.Acima
C#, as was answered here, is first compiled into Intermidiate Language , and only after compiled in machine specific machine code. There is a reason check Eric Lippert answer #7875753 to understand the reasons behind that.Thrum
F
0

C#, like Java, has a hybrid language processor. Hybrid processors perform the jobs of both interpretation and compilation.

Fivespot answered 14/3, 2015 at 18:18 Comment(0)
R
0

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

Runofthemine answered 29/8, 2017 at 11:20 Comment(1)
By this definition QBasic was a compiler. That's clearly a crock. C# is also an interpreted language, with some JITting here and there as an optimization. Sure, you can NGen .Net IL too, but you lose the optimization the JITter can do. What you get in IL is a list of data used to dispatch calls into subroutines, no machine code is generated or executed except where the runtime sees heavy use and replaces runs of the p-code data by JITted machine code to replace the call dispatching.Hathaway
E
0

Great debate going on here. I have read all the answers and want to express some conclusions, based on my research and Programming language implementation concept.

There is a concept of Programming language implementation.

Programming language implementation: In computer programming, a programming language implementation is a system for executing computer programs. There are two general approaches to programming language implementation:

  1. Compilation:

    The program is read by a compiler, which translates it into some other language, such as bytecode or machine code. The translated code may either be directly executed by hardware, or serve as input to another interpreter or another compiler.

  2. Interpretation:

    An interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program.

    • Parse the source code and perform its behavior directly.
    • Translate source code into some efficient intermediate representation or object code and immediately execute that.
    • Explicitly execute stored precompiled bytecode made by a compiler and matched with the interpreter Virtual Machine.

Conclusions:

  • So any language which converts the code to intermediate byte code or machine code are compiled languages.
  • There are multiple types of Interpreters like Byte Code Interpreters, Just-in-time interpreters, etc.

Famous Compiled Languages:

  • JAVA C# C C++ GO Kotlin Rust

Famous Interpreted Languages:

  • JavaScript PHP Python

Directly Compiled Languages v/s Languages which are compiled to bytecode first:

  • Bytecode interpreters (virtual machines) are generally slower than direct execution of machine code. Any interpreter has some overhead when converting bytecode to actual machine instructions.
  • Interpreters do line by line execution.
  • So, directly compiled languages like C++/C are faster then Java/C#
Eft answered 10/6, 2022 at 19:45 Comment(1)
considering this im now 100% sure c# is a compiled language and python is interpreted.the whole debate here made me fuzzy about the whole interpretation but now it makes sense considering i can just write files using the existing modules in python and dont have to stop the application to translate them to use them, and thats because the interpreter is doing its job anyway on a separate area.Puritanism
D
0

There are an implementation of C# that is a compiled language.

It is Remobjects c# island that compiles directly to binary machine code and run without an VM and without an Runtime but uses directly platform API (win32 on microsoft, Cocoa on apple and posix on linux).

Also remobjects c# interoperate directly with C/C++ compiled librarres because it's calling convention translate to c calling convention.

Dodecahedron answered 26/6, 2022 at 14:40 Comment(1)
doesnt matter what implementation you talk about, c# cannot run unless it is compiled, and it cant be added to because the interpreter cannot read the added c# code directly without the compiled code being updated. therefore the whole idea of c# being interpreted is bogus.Puritanism

© 2022 - 2024 — McMap. All rights reserved.