15

I have read a lot of controversy about C#, where some say it's interpreted, some say it's not. I do know it's compiled into the MSIL and then JITed when run, depending on the processor etc...but isn't it still interpreted in the way it needs a VM (.NET) to run?

7
  • 5
    Who ever defined "interpreted == needs a VM"? One can interpret from source code without defining a VM (some PLT guys might argue every language is a VM, but that doesn't fit the general usage), one can have a "VM" that's defined abstractly but only implemented by compilation to some machine code dialect. Besides, if anything, MSIL would be interpreted - C# is obviously compiled to MSIL, even ahead-of-time.
    – user395760
    Commented Mar 4, 2012 at 15:58
  • 1
    I guess you could ask the same question about java
    – sebagomez
    Commented Mar 4, 2012 at 15:59
  • 7
    There is no "controversy". Controversy implies that there is debate. There are just facts- .NET uses IL (and a VM to JIT compile that IL). If you want to call this being interpreted, that's fine, but you need to keep in mind that the typical associations with "interpreted-ness" (relatively poor performance, dynamic typing, late-binding) do not apply to .NET, and so the value of this classification is dubious. Commented Mar 4, 2012 at 16:12
  • 2
    This might help: blogs.msdn.com/b/ericlippert/archive/2011/11/18/why-il.aspx Commented Mar 4, 2012 at 17:09
  • 1
    @GETah, lol, Java is about as portable as a brick these days.
    – code4life
    Commented Mar 5, 2012 at 16:29

3 Answers 3

39

The VM is an abstraction of a microprocessor. It is just a definition and does not really exist. I.e. you cannot run code on the VM; however, you can generate IL code for it. The advantage is that language compilers do not need to know details about different kinds of real processors. Since different .NET languages like C# or VB (and many more) produce IL, they are compatible on this level. This, together with other conventions like a common type system, allows you to use a DLL generated from VB code in a C# program, for instance.

The IL is compiled just in time on Windows when you run a .NET application and can also be compiled ahead of time in Mono and in .NET starting with .NET 8 (see: Native AOT deployment). In both cases, native machine code for the actual processor is generated. This fully compiled code is executed on the REAL microprocessor!


A different aspect is the number of compliers you have to write. If you have n languages and you want to run them on m processor architectures, you need n language-to-IL compliers + m IL-to-native-code compliers. Without this intermediate abstraction layer you would need to have n × m compliers and this can be a much higher number than just n + m!

2
  • 1
    "This fully compiled code is executed on the REAL microprocessor"... true, but additional code, not derived from the IL, implementing behavior of the virtual machine is executed also (for C#, this would be the .NET Common Language Runtime)
    – Ben Voigt
    Commented Nov 16, 2016 at 21:06
  • 1
    Yes, the CLR manages things like loading, linking JIT-compilation, security aspects, memory management and more. But I would compare it to a kind of operating system, rather than to a VM. Commented Nov 16, 2016 at 21:18
2

The short answer is no, the requirement for the VM does not indicate that it's interpreted.

The VM contains the JIT compiler that translates IL to native machine code. It also contains the .NET class library, upon which C# programs depend. It also contains some other mechanisms involved in dynamic linking and such (definitely built on top of Windows DLL mechanism, but .NET has features above and beyond what Windows provides on its own, which are implemented in the VM).

2

You are probably refering to CLR (an implementation of the specification CLI).

The CLI defines a specific type system, semantics of all the operations on these types, a memory model, and run-time metadata.

In order to provide all of the above, some instrumentation of the generated code must happen. One simple example is to ensure that larger-than-32-bit numerals are supported and that floating-point operations behave as per specification on every architecture.

In addition, to ensure correct behaviour of memory allocation, correct management of metadata, static initialisation, generic type instantiation and similar some additional processes must be present during the execution of your CLR code. This is all taken care of by the VM and is not readily provided by the CPU.

A quote from Wikipedia, for example:

The CLR provides additional services including memory management, type safety and exception handling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.