CodeDigest.Com Logo
Featured:

What is .Net Standard ?

.Net Standard is a specification which dictates what the Base Class Libraries of different .Net platforms should implement to unify the Base Class Libraries of different .Net Platforms. Here, Platform means full .NetFramework, .Net Core, Xamarin, Silverlight, XBox etc. This also enables code charing between applications that runs on these different platforms. For example, a library or a component that is developed on top of a platform that implements specific .Net Standard version can be shared by all the applications that runs on any of the platforms that implements same .Net Standard version.

The project home is at the GitHub repository here.

Is it a Framework of its own?

.Net Standard is not a framework or platform of its own. It does not have implementations or a runtime, it just defines a specification what different .Net platforms has to implement to remain .Net Standard complaint. For Instance, .Net Core is a framework with runtime that implements .NetStandard.

In other words, .Net Standard defines a set of API’s that the platforms has to implement to be complaint with .Net Standard. By implementing this specification, the base class libraries of different platforms are unified which were otherwise evolving independently though many of the library functions are common. On the other hand, this also meand the developers need not learn different Base Class Libraries for learning different platforms.

Why We Need .Net Standard?

When .NetFramework was released, it served as a general application development platform for windows desktop and server based environments. After this, a new version of .NetFramework called .Net Compact framework was introduced for mobile platforms. Similarly, introduction of Silverlight, Windows 8 apps platforms, etc. added their own flavors of .Net Framework and runtime. So, there were many verticals for .Net Platforms with similar base class libraries that were evolving separately and delivered by separate teams in Microsoft. For developers, sharing a library code or component between this platforms requires to re-implement and build it for the specific platform though they were providing same feature. To address this problem, a new way of compiling libraries were introduced which is called Portable Class Library.

What is Portable Class Library?

Portable Class Libraries are introduced to solve this code sharing problems. It is a re-usable library project where we specify the .Net Platforms we are planning to target during creation of library. This project has allowed developers to build portable libraries which exposed only those Base Class Libraries (or API’s) that are common between the selected platforms. The Visual Studio tools will manage this for us. For example, if you select the target platforms as .NetFramework and Silverlight, the library can then use only those API’s which are commonly available in both of this platforms. Refer the below Venn diagram,

Assuming, we are creating a portable class library project that targets .Net 4.5, Silverlight and Xbox platforms, the library project can use only the common libraries (BCL subset in Venn diagram) that are obtained by intersection of all platforms BCL. So, higher the number of platforms then lower the surface (number) of common API.

In Visual Studio 2012, we have a project type called Portable Class Library project for this. Refer below.

Creating this project will ask you to select target platforms the library is targeting like below.

The above selection will create a portable library class that allows us to consume only the API's that are common between .NetFramework 4.5.Silverlight 4, Windows Phone Silverlight 7 and Windows 8.

Though the portable libraries allowed code sharing between multiple platforms, it still had disadvantages like, adding a new platform support requires re-compiling against the new set of target platforms and removing the API references that are not part of new intersection of base class libraries. Portable Class Libraries also does not have any control on the target platforms which were still evolving separately.

Introduction to .Net Standard

.Net Standard has solved all this in a different way, it provided an API specification which all platforms should implement to remain .Net Standard complaint. This has unified the base class libraries of different .Net platforms and has paved way to share libraries and also brought the BCL evolution centralized as seen below.

Image Reference: https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/

So, the introduction of .NetStandard now replaces Portable Class Libraries for creating re-usable libraries.

.Net Standard Versions and Platform Support

The .NetStandard is versioned similar to framework versioning. The current version of .Net Standard is 1.6 and .Net Core 1.0, 1.1 supports .Net Standard 1.6. Higher the version number then higher the number of API it supports. Currently, .Net Core is sort of reference implementation for .NetStandard specification. A new version of .NetStandard is accompanied by a .Net Core release. The upcoming version of .NetStandard is 2.0 and the upcoming version of .Net Core is likely to support 2.0. Refer the below .NetStandard and Supported Platforms matrix to understand what version of platforms implement what version of .NetStandard.

The above matrix is maintained and updated on .NetStandard GitHub repository here.

For example, to develop a library that supports .NetFramework 4.5.1 and .Net Core 1.0, we need to target .Net Standard 1.2 i.e. the lowest .NetStandard version that the 2 frameworks implement.

The Nuget packages now has .NetStandard version support information on Nuget.org. For instance, JSON.Net supports the below .NetStandard as of now,

Currently, .Net Core framework has a very limited API surface when compared to full .NetFramework. So, the .NetStandard 2.0 release is heavily anticipated which will add almost every API that full .NetFramework currently supports into .Net Core.

What are not part of .Net Standard?

.NetStandard has only the Base Class Libraries that can be shared across platforms except few cases which will throw NotImplemented exception. The Application model specific API’s commonly called Framework Class Libraries (FCL) like WebForms, Windows Forms, WPF, WCF, etc. and OS specific API’s like Registry, AppDomain, etc. are out of the .NetStandard specification to make the common BCL remain platform agnostic.

Creating .NetStandard Library in Visual Studio 2017

Visual Studio 2017 has new project Template called .Net Standard as seen below to create .NetStandard libraries.

This project type only includes BCL that are part of .NetStandard version. You can see this in SDK property window as seen below.

We can change .NetStandard version from the project Properties > Application tab as seen below.

Happy Learning!!