With so many different platforms, standards, and frameworks, how do you make sense of it all? There is .NET Framework, .NET Core, and .NET Standard before you even get to Mono and Xamarin.
What is the difference, which do I need, what is .NET Standard? When do I use .NET Standard? If you're confused, then you're not alone and this post is for you.
In this post, I explain .NET Standard. You'll learn the purpose of .NET Standard, what it is, why it exists, when to use it, and how to use it. You'll also learn when to use platforms like .NET Core and .NET Framework.
What is .NET Standard?
The first thing you need to know about .NET Standard is that .NET Standard is not a framework or platform at all. It is fundamentally different from .NET Core, .NET Framework, Mono, and Xamarin. This confuses many people, so if you're confused about it, that's normal.
Here is a simple explanation by way of analogy:
- .NET Standard is an interface.
- .NET Core and .NET Framework are implementations of that interface.
Now let us examine this in more detail. A particular version of .NET Standard like .NET Standard 2.0 is a set of APIs without implementations. For a particular version of a framework like .NET Core 2.0 or .NET Framework 4.7 to implement .NET Standard 2.0, it needs to provide implementations for all the APIs in .NET Standard 2.0. If it does, then it supports that version of .NET Standard.
Targets and References
.NET Framework 4.6.1 and higher implement .NET Standard 2.0, as does .NET Core 2.0. When your application targets one of these platforms, for example, .NET Core 2.0, we say that your application is running on the .NET Core 2.0 platform.
An application that is running on the .NET Core 2.0 platform can reference any libraries that target .NET Core 2.0, .NET Standard 2.0 (as the .NET Core 2.0 framework implements it), or any other version of .NET Standard that .NET Core 2.0 implements (for example, .NET Standard 1.0).
Just like a class, each framework can implement multiple interfaces. The following diagram shows that .NET Core 2.0 implements .NET Standard 1.0, 1.5, and 2.0, whereas .NET Core 1.0 only implements .NET Standard 1.0 and 1.5, and finally that .NET Framework 4.5 only implements .NET Standard 1.0.
There are more frameworks and more versions of .NET Standard than shown in this diagram. To find out what versions of .NET Standard are implemented by each version of each platform, refer to the .NET Standard Versions table.
Why does .NET Standard exist?
.NET Standard was created to unify all the different .NET platforms and solve problems with the old solution, Portable Class Libraries.
Portable Class Libraries (PCLs)
Previously, each platform implemented different APIs. This made it difficult to build cross-platform libraries, as there was no consistent set of APIs that you could use. If you needed to support a new platform, it might not implement some of the APIs you were using.
The old solution to this problem was Portable Class Libraries. The problem with those was that the APIs you could use were dependent on which platforms you targeted — you could only use the intersection of the platform APIs.
Another issue with PCLs was that you needed to recompile them with a different PCL profile to support new platforms. This meant that when a new platform was released, existing libraries could not be used until they were recompiled.
However, it was even worse than this, if a library was dependent on another library, then it couldn't support a new platform until its dependency supported it too. In the case of abandoned or functionally complete libraries, they often never got support for new platforms. This meant that getting support for new platforms was a long drawn out process. This long process often stopped or delayed adoption of new platforms in existing projects with hard dependencies on existing libraries.
The goal of .NET Standard is to make it easier to create reusable libraries. Ideally, you should be able to write one library and have it work with .NET Framework, .NET Core, Mono, Xamarin, and any other platforms that may exist in the future.
.NET Standard achieves this goal by defining a common shared set of APIs that all platforms implement. In this way, you can target one specification, a particular version of .NET Standard, and have your code support any platform that implements that specification.
This leads to another big benefit with .NET Standard libraries. You do not need to recompile them to support new platforms. .NET Standard libraries work automatically on any platform that implements the version of .NET Standard that your library targets.
.NET Standard 2.0 brings one great new feature to the mix. It adds a compatibility mode that allows .NET Standard projects to reference .NET Framework libraries.
This compatibility mode works when the .NET Framework library does not use any APIs that are beyond the scope of .NET Standard 2.0. For example, .NET Framework libraries that use WPF are incompatible.
This new feature is quite widely applicable, as most .NET Framework libraries are compatible. Approximately 70% of NuGet packages targeting .NET Framework are compatible.
When do I use .NET Standard?
You target .NET Standard when you are building a library. Particularly, when building a library that you want to use in multiple projects or that you want 3rd parties to consume.
You do not target .NET Standard when you're building an application. If you're creating an application, for example, a console application or an ASP.NET Core web application, you target a platform like .NET Core.
When building an application, you'll usually want to target the newest version, for example .NET Core 2.0. This gives you the largest API and the greatest compatibility with the libraries you might want to consume.
Libraries targeting Specifications
When you're building a library, you target a specific specification such as .NET Standard 2.0.
Building a library is in some ways, the opposite of building an application. Instead of writing your library to target the newest version of .NET Standard, you want to target the oldest version that has the APIs you need. This increases the compatibility of your library and maximizes the number of potential consumers of your library.
As the goal is to target the oldest version, .NET Standard versions never go out of date. Therefore, it is more proper to refer to them as specifications instead of versions. For example, .NET Core 2.0 implements the .NET Standard 2.0 specification.
Libraries targeting Frameworks
You can also target your libraries at frameworks like .NET Framework 4.7. However, you should not do this, unless you really need some API that is only available in that framework. That is because frameworks are not the lowest common denominator, that's .NET Standard's role.
If you do target your library at a particular framework version, for example, .NET Framework 4.7, then you're limiting the applications that can use your library to applications running on .NET Framework 4.7 or higher. Applications on .NET Framework 4.6.1 will not be able to consume your library.
The only real reason to target a framework, is if you're building a framework specific library, for example, one that extends WPF. However, in many cases, you'll still be able to target .NET Standard and then simply take additional dependencies for any specific requirements, such as ASP.NET Core.
How do I use .NET Standard?
To use .NET Standard, start by creating a new .NET Standard Class Library project.
Then right click on the project in Solution Explorer and select Properties. Go to the Application tab and change the target framework to the .NET Standard version you want to target.
You have now created a new library targeting .NET Standard.
Installing .NET Standard 2.0
If you've upgraded to Visual Studio 2017 v15.3, you may find that you're missing .NET Standard 2.0 and .NET Core 2.0. Unfortunately, these do not come preinstalled with the Visual Studio 2017 update.
Instead, you need to download and install the .NET Core 2.0 SDK. After installing the SDK, you'll be able to target .NET Standard 2.0 and .NET Core 2.0.
.NET Standard is an interface, a set of APIs without implementations that a framework later implements.
.NET Standard unifies all the .NET platforms and solves the problems with the old Portable Class Libraries. .NET Standard 2.0 adds a compatibility mode that adds support for older, but compatible, .NET Framework libraries.
Your applications should target a platform such as .NET Core or .NET Framework. Your libraries should target the lowest .NET Standard specification possible, or much less often, a framework.