I was watching the Microsoft build conference from October last year and I noticed they announced that for building the new Metro style apps, developers can write native code in C#. How is this possible? I'm just curious. Wasn't C# designed (as part of CLI standards) to first be compiled to intermediate byte code and then run on a virtual machine? How can something that runs on a virtual machine be called "native code"?
-
1bytecode is native to the virtual machine; that's the whole idea – Mateusz Kowalczyk Jun 23 '12 at 06:38
-
C# is a language. Do you think there's some magic fundamental to it that would prevent it being compiled to native code? It doesn't have to run on a VM. – Damien_The_Unbeliever Jun 23 '12 at 06:39
-
@MateuszKowalczyk so its not native code in the "real" sense, is it? – Ayush Jun 23 '12 at 06:40
-
I think it just means they are giving you access to the same API they are using or at least offering a public API ... I think metro apps can also be created using HTML/JS http://msdn.microsoft.com/en-us/library/windows/apps/br211385.aspx – Samy Vilar Jun 23 '12 at 06:41
-
@Damien_The_Unbeliever I'm still a CS student so I wouldn't really know even if there was some "magic fundamental". So by that logic, Java code can compiled to native code too? And if that's possible, why is it that we have never seen a native code compiler for Java? For example, even if you use the GTK+ bindings for Java (for developing linux applications), the JVM is still needed to run the application. Like I said, I'm just curious about these things. – Ayush Jun 23 '12 at 06:44
-
2You are mistaken. WinRT opens a development path for developers to write native code using C / C++, not C#. Native (x86) code development in C# is impossible unless someone goes through the trouble of writing a compiler to do exactly that. WinRT applications coded in C# still use .net and run on the CLR. – Chris Dworetzky Jun 23 '12 at 06:46
-
2Depends on your interpretation of 'native code'. There isn't really a set-in-stone definition and the term is used very loosely. – Mateusz Kowalczyk Jun 23 '12 at 06:46
-
2@Ayush JVM is there for a reason. One of the biggest selling points of Java is the JVM. As long as someone writes and compile a JVM on your machine, you can execute Java code written anywhere. This means that you don't have to write a compiler for each architecture and the compiler might not work the same across different machines. Instead you write a JVM and you can be fairly sure that the code you wrote somewhere else will run the same. Also, there exist Java compilers to machine code. [See this Wiki page](http://en.wikipedia.org/wiki/Java_compiler) – Mateusz Kowalczyk Jun 23 '12 at 06:50
-
@FurDworetzky Thanks for explaining that. If I may ask another question, is Mono a different kind of C# implementation as compared to .NET? I've heard people say that one can develop native GTK apps using C# (Mono). Is that correct? – Ayush Jun 23 '12 at 08:05
-
@Ayush Mono is just an open-source implementation of the CLR. It doesn't allow compilation to x86 machine code. Likely people are using a different interpretation of the word 'native' in that context. – Chris Dworetzky Jun 23 '12 at 08:14
2 Answers
It has always been possible to call native code from managed code. Generally any .net app that interacts with windows at some point is handing control over to native components.
Historically, to manually invoke native code we would use PInvoke (Platform Invocation) - which allows managed code to call unmanaged functions that are implemented in a DLL.
What's new now is Windows Runtime, basically an ehanced COM-based API. You can read up about it here - http://en.wikipedia.org/wiki/Windows_Runtime - its just making the process easier / less overheard.
EDIT - to be clear, the presentation was not that c# could be compiled to native code. this is still not possible. they were touting winrt as providing a new (cleaner) mechanism for including calls to native code in c#.

- 1,444
- 1
- 11
- 22
-
1From reading the question, the OP seemed to me to be asking about compiling C# to machine bytecode, not invoking non-managed libraries. I could be wrong, though. – Chris Dworetzky Jun 23 '12 at 06:57
-
2He misunderstood the presentation. You cannot compile c# to native code (theoretically you could build such a compiler, it does not exist however.) However it is supposedly easier to combine c# with native code. For an example see http://blogs.msdn.com/b/pfxteam/archive/2011/11/12/10236375.aspx – bkr Jun 23 '12 at 07:00
-
The Bartok compiler (part of the Singularity project by MS from ~5 years ago) compiled C# (well, Sing#, but it's an extension of C#) to native code, IIRC. – Damien_The_Unbeliever Jun 23 '12 at 07:30
-
that was interesting... I'd never heard of Bartok before, although reading up about it, it sounds like it is in fact still managed. – bkr Jun 23 '12 at 07:39
The CoreRT project is an example of trying to natively compile C#.

- 57,289
- 29
- 176
- 237

- 61
- 1
- 5