Class library projects - .NET Framework, .NET Core, and .NET Standard
Recently Microsoft
announced the release of .NET Standard 2.1. You might have already noticed
different project templates in Visual Studio 2017 that allow you to build class
libraries such as .NET Framework class library, .NET Core class library, and
.NET Standard class library. As a beginner you might have wondered what exactly
is the difference between them. To that end this article briefly explains each
of these three types of class libraries.
Understanding the terms - .NET Framework, .NET Core, and .NET Standard
Before we go ahead and discuss the flavors of class library projects, let's
first quickly understand the three terms - .NET Framework, .NET Core, and .NET
Standard.
.NET Framework is implementation of .NET that runs on
Windows platform. This is the flavor of .NET we all know for a long time. It
allows you to build windows desktop applications as well as ASP.NET web forms
and ASP.NET MVC applications. .NET framework was first released in 2002 and its
current stable version is 4.7.
.NET Core is a new modular framework that is cross-platform
and open source. It allows you to build console apps and ASP.NET Core apps. .NET
Core was released in 2016 and its current stable version is 2.1.
Besides .NET Framework and .NET Core there is Mono based runtime - Xamarin -
that allows you to build mobile apps (iOS and Android etc.) .
.NET Standard is a set of APIs that all .NET platforms need
to implement. The latest version of .NET Standard specification is 2.1. The .NET
Framework 4.7 and .NET Core 2.1 implement .NET Standard 2.0. The .NET Framework
4.8 will continue to implement .NET Standard 2.0 and .NET Core 3.0 (and also
Mono and Xamarin) will implement .NET Standard 2.1.
Creating class library projects
If you see Visual Studio 2017 project templates you will find three distinct
templates as follows :
- Class Library (.NET Framework) : This can be found under Visual C# >
Windows Desktop option of the New Project dialog.
- Class Library (.NET Core) : This can be found under Visual C# > .NET
Core option of the New Project dialog.
- Class Library (.NET Standard) : This can be found under Visual C# > .NET
Standard option of the New Project dialog.
As an example the following figure shows the .NET Standard class library in
the New Project dialog of Visual Studio 2017.
Now you might be wondering - Which flavor to pick out of these three?
The answer to this question depends on what you are trying to achieve. It is
about platforms you wish to target vs. platform specific APIs.
If your application is intended to run on .NET Framework alone then the first
option is what you need. If you want to target .NET Core alone then the second
option is what you need. However, if you want your code to run on all the
available .NET platforms - .NET Framework, .NET Core, and Xamarin (Mono) - then
you will need to go for the third option.
So, basically going for .NET Standard class library increases your platform
support whereas picking a class library flavor targeted for a particular .NET
platform will allow you to use all the platform features for that platform.
The following figure shows how all these three types of class library
projects look like in Solution Explorer :
Setting the .NET Standard version
When you create a new .NET Standard class library project by default it
targets .NET Standard 2.0 specifications. If you wish to change the target
version you can do so in project property pages.
You can also set the target versions for .NET Framework and .NET Core class
library projects in similar way.
Target Framework Monikers (TFM)
The target frameworks discussed above have what is known as Target Framework
Moniker (TFM). It's basically an alias used for a particular framework. For
example, .NET Framework has TFMs such as net46 (.NET Framework 4.6) and net47
(.NET Framework 4.7). TFMs used for .NET Core are - netcoreapp2.0 (.NET Core
2.0) and netcoreapp2.1 (.NET Core 2.1). Finally, TFMs for .NET Standard include
- netstanadrd1.6 (.NET Standard 1.6) and netstandard2.0 (.NET Standard 2.0).
TFMs are often mentioned in the project *.csproj files. For example, have a
look at the following markup from a sample *.csproj :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
...
...
</Project>
That's it for now! Keep coding!!
For more details about .NET Standard you may go
here and
here.