C# is a compiled or interpreted language?

Asked

Viewed 6,154 times

33

I’m starting my studies in C#, and I’m 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.

  • C# compiled language

  • I also want to know... I just saw on google (Wikipedia) they talking that is Interpreted, but the guy at the lecture said it’s compiled

  • 1
  • @Wallacemaxters cite the source that says about the interpretation.

  • https://pt.wikipedia.org/wiki/C_Sharp

  • 3

    @Wallacemaxters our the Wikepdia in Portugese is full of errors. And the worst thing you’re trying to solve is there’s always a guy who thinks he owns the article and won’t let it move. That’s why I say you have to look at everything in English: https://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29

  • 4

    @Bigown I’m tired of arguing with these WP guys in Portuguese, I don’t even touch there anymore. I’ve had several bugfixes reverted to the bug version. There is a dummy there that I will not say the name, but is very famous in the Portuguese version that wants to move himself. You have to be "negotiating" the post and the guy makes sure to appear at all.

  • @Bacco happens the same in English wikipedia - you make a correction and it is reversed. The culture in both languages is the same - the illusion that the English wiki is more correct or safer. In English it has more content, but has a lot of tosquice in both languages.

  • C# just like Java is a compiled language and requires a compiler to run its program

  • C# as well as Java is an interpreted language, where when compiling (build) is generated an intermediate source code that will be interpreted by the virtual machine of the language. This intermediate source code has built-in compilers that, at runtime, transform intermediate code into machine language. EDIT-ok, I read the answer marked as the best answer and I realized my failure in the comment above.

Show 5 more comments

3 answers

36


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

Well, that’s the simple explanation.

None programming language is compiled or interpreted. Language is something abstract. Just like Java, C# is usually used in compiled form. But this is an implementation detail, nothing requires it to be like this.

That can also be observed in JS, where everyone thinks they are being interpreted (and it is not totally wrong).

In fact C# can be used in an interpreted way (in general people do not use it except as REPL interactive (see more).

What makes people think that C# is interpreted is that it doesn’t usually generate native code directly in the standard way of using it. This is done by a Jitter (Wikipedia) in a second moment. Jitter is a compiler that generates machine code when running. In the case of the platform. NET is made on top of a simpler intermediate code of "interpret". But make no mistake, after going through Jitter, there is no interpretation.

Normal is . NET running on a virtual machine, one Runtime. It is a mechanism that controls every execution of the application. Some virtual machines are interpreted. This is not the case for C#. It takes the intermediate code (bytecode) produced by the main C# compiler and turns into machine code in memory. This is done once at execution and then uses the same code would be in C, C++, Pascal, etc..

There’s a tool called Ngen which compiles this and generates a native executable for direct use without going through Jitter. This is done after the application is installed. NET, thus gains time in the executable load. There may be other types of gain, but there may be some losses because the code ceases to be adaptive, unless you use a global optimizer based on execution statistics. In the latest versions of . NET Core this form has been expanded and integrated into the normal construction of the application. And more recently still the ability to generate something AOT has been expanded.

But nothing prevents the code from being native from the beginning. The Mono implements C# natively, mainly in Android and iOS that require it. Already exists the .NET Native. And several other native code implementations are being developed for .NET Core (LLILC, IL2CPP, among others). This is a bit outdated, the standard . NET already has AOT capacity.

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

Interpreting

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

In fact purely interpreted languages hardly exist anymore, at least among the mainstream. The most common is to have at least one compilation for an intermediate code. What we usually say is interpreted is when the source is always needed to execute. But this definition is very complicated to do. JS, for example, is interpreted in the sense that it has to analyze the whole code in each execution. But it runs native because after this interpretation it runs native. At least that’s how it works in the best known implementations.

Note that almost 100% of languages with interpreted implementation are dynamic in their typing and even form of execution. Often this carries a greater weight until the interpretation.

  • I gave +1 and I await the details.

  • Thank you for the excellent reply!

7

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

  • 5

    Read my answer and follow the links to better understand what an interpreted language is. C# is not interpreted.

6

Be careful 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 identical IL code. For ASP . NET this first build step can happen automatically when the page is first requested, or you can run it in advance (a process known as pre-compilation). The compiled file with IL code is an Assembly called."

Here is one of the great magic of the . net framework which is the possibility of programming in the language that suits you to compile for a code that is interpreted by the computer without having to modify or adapt its code.

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

Good studies.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.