Is C # a compiled or interpreted language?

31

I'm starting C # studies, and am I wondering if the language is compiled or interpreted?

My doubt arises because I heard in a lecture that it is compiled, and others saying that it is interpreted.

    
asked by Miguel Batista 27.04.2016 в 17:00

3 answers

31

It is completely compiled. First for an intermediate code, then for native code.

Well, this is the simple explanation.

No programming language is compiled or interpreted. Language is something abstract . Like Java , C # is usually used compiled. But this is an implementation detail. Nothing requires it to be like this.

This can also be seen in JS , where everyone thinks they will be interpreted (and not entirely wrong).

Actually C # can be used in an interpreted way (usually people do not use it except interactive REPL ( see more ).

What makes people think that C # is interpreted is that it does not usually generate direct native code. This is done by a JITter ( Wikipedia ) in a second moment. JITter is a compiler that generates machine code at the time of execution. In the case of the .NET platform it is done on top of a simpler "interpret" intermediate code. But make no mistake, after going through JITTer, there is no interpretation.

It's normal for .NET to run on a virtual machine , an runtime . It is a mechanism that controls all application execution. Some virtual machines are interpreted. Not the case with C #. It takes the intermediate code ( bytecode ) produced by the C # main compiler and transforms it into machine code into memory. This is done once in execution and then uses the same code would be in C, C ++, Pascal , etc.

There is a tool called Ngen that does this build and generates a native executable for direct use without going through the JITter. This is done after installing the .NET application, so it takes time to load the executable. There may be other gain types, but there may be some losses because the code is no longer adaptive, unless you use a global optimizer based on execution statistics .

But nothing prevents the code from being native from the beginning. Mono natively implements C #, especially in Android and iOS that require this. .NET Native already exists. And several other native code implementations are being developed for .NET Core (LLILC , IL2CPP , among others).

But make no mistake, this code that is not initially native has nothing to do with interpretation.

Interpretation

Another important detail is that every interpretation in the background goes through a compilation process . The difference is that the interpreter already executes the code that it found and the normal compiler generates a transformed code that will be executed later. So the concept of interpretation must be well understood. The problem of the interpretation is to be doing the analysis of the source code all the time.

In fact purely interpreted languages almost no longer exist, at least among the mainstream . The most common is to have at least one build for an intermediate code. What is usually said to be interpreted is when the source is always needed to execute. But this definition is very complicated. JS, for example is interpreted in the sense that it has to parse the whole code in each execution. But it runs native because after that interpretation it runs native. At least this is the case with most popular implementations.

Note that almost 100% of interpreted implementation languages are dynamic in typing and even execution. Many times this brings a greater weight to the interpretation.

    
27.04.2016 / 17:04
7

C # has its source codes transformed into an intermediate language (language-specific), which will be interpreted by the language's virtual machine when the program is run. from there we can say that it is an interpreted language.

    
27.04.2016 в 18:49
5

Caution when comparing Asp.net with C # asp.net is also compiled but has a way of running interpreted as web-site-type projects and not web application.

"Essentially all .NET languages (including Visual Basic, C #) are compiled into IL-identical IL code. For ASP.NET this first compile step can happen automatically when the page is first requested, or you can execute it in advance (a process known as precompile) .The file compiled with IL code is a so-called assembly. "

Here is one of the great magics of the .net framework that is the possibility to program in the language that best suits you to compile for a code that is interpreted by the computer without having to modify or adapt your code.

Remembering that you can consume C # components made in Vb.Net, J #, Cobol.Net and so on. And all these languages consume components built in C #.

Good studies.

    
03.05.2016 в 19:57