Is C# platform independent?

C# is one of the most popular languages out there. It has many great advantages, such as being easily readable, quick to write code in, simple in terms of implementing features, garbage-collected and so on. But one of its greatest advantages is being portable.

How does it achieve portability?

C# runs on the so-called Common Language Runtime (CLR, or CoreCLR for .NET Core). This runtime is a virtual machine that manages the execution of .NET executables. After you write your C# code, you compile it. It gets compiled to an Intermediate Language. This language is an easy-to-understand-and-work-with version of the source code for the virtual machine. When you want to run your .NET executable (.dll or .exe file), the CLR steps in. It uses Just-In-Time (JIT) compilation to translate the Intermediate Language into machine code while optimizing it for better performance. This is all done to increase portability, because once you compile the code with .NET Core compiler, you can run it on any platform (or architecture) which .NET Core supports. And that’s the reason why only rewriting the runtime is necessary in order to support a new platform, and every existing .NET Core application will work with it. It also has other advantages such as added memory safety, type safety, exception handling and improved security. Other examples of CLR languages are C++/CLI, F#, Visual BASIC and PowerShell. Another language that works on a similar principle is Java, with the only difference being that Java source code is translated to Java bytecode, designed to be run by the Java Virtual Machine (JVM).

So… It’s all fine and dandy, right?

Well, no. C#, in its current state, has three runtimes on which it relies. The first one being .NET Framework, which is closed-source and Windows-only. The second one – Mono, which is open-source, supports variety of platforms, including Linux, macOS, Windows, Xbox360, PlayStation 3 and Android, but is not fully compatible with .NET Framework. And the third one – .NET Core, is open-source, supports Linux, macOS and Windows, but is a rather new runtime (it was released in 2016) compared to .NET Framework (2002) and Mono (2004), which means very few programs are written for it and its general usage is still low. This makes C# platform dependent in a sense. Or rather runtime dependent.

But with the future release of C# 8.0, Microsoft has planned to merge the three frameworks under the name “.NET 5”, make the other two obsolete and put C# one step closer to being a truly platform independent language.

Summary

Although C# is currently mainly used for Windows-based development, Microsoft plans to change that soon with the release of .NET 5. .NET Core has been gaining a lot of popularity since its release, and this is a trend set to continue in the future.

If you want to try out programming with C# and become a successful .NET Core developer, take the first step by applying for the Free Programming Basics Course from HERE.

Author: Teodor Zlatkov