What enables code written in C#, F#, and VB.NET to run within the same application? And what is .NET anyway?
A Standardized Specification
When I was working on a legacy website developed in VB.NET, I had to implement PayPal Payments Pro. After some searching, I found out that the PayPal team provided several SDKs for different platforms, including .NET, to consume their API. And, guess what? The .NET SDK was developed in C#… but in the .NET world, that’s not a problem. I just referenced the dll (compiled .NET library, similar to the Java equivalent .jar containing .class files) in my project and was good to go. No need to build a client API and re-invent what was already implemented by brilliant developers!
All thanks to the CLI.
The ECMA Common Language Infrastructure (CLI) is an international standard that gives a specification for executable code and the execution environment. The CLI sets standards and rules to allow different languages to be used and run regardless of the operating system, relying on the virtual execution system in which it runs. The elements specified in the standard are: the Common Type System (CTS), the Common Language Specification (CLS), Metadata, Portable file system, Common Intermediate Language (CIL), and the Virtual Execution System (VES).
.NET as a Proprietary Implementation of the Specification
Ok, fair enough, but what does .NET have to do with this? The .NET framework is Microsoft’s implementation of the CLI. Through the platform-specific Common Language Runtime (CLR) .NET provides the VES which among other things, compiles Intermediate Language code (assembly code) to machine-readable code.
The CLR is somewhat equivalent to the Java Virtual Machine as it compiles the Intermediate Language code, previously compiled from C# or F# into machine code. But the CLR does more than that: it provides services to managed code such as exception handling, garbage collection, security… But, what is managed code? Managed code is code written in a language that is compliant with the CLS specification such as C# or .Managed C++, and thus can be compiled to Intermediate Language. (read more)
To sum up, what makes possible for C#, F#, J#, VB.Net, Clojure , and other .NET languages to run together is that they are all compiled to the identical intermediate code. The compilation of code in .NET is done twice: first to compile from the language specific code to the Common Intermediate Language; and, secondly from the CIL to native machine code at runtime.
If you want to explore other implementations of the Common Language Infrastructure specification, you can have a look at the open source Mono platform, or at Microsoft’s open source implementation .NET Core.
This article was first published on the Codurance blog.