Before .NET Standard, there was Portable Class Libraries (PCL). With PCLs, you can create a library of code and explicitly specify which platforms that you want the library to support, such as Xamarin, Silverlight, Windows 8, and so on. Your library can then use the intersection of APIs that are supported by the specified platforms.
Microsoft has realized that this is unsustainable, so they have been working on .NET Standard---a single API that all future .NET platforms will support.
If you want to create a library of types that will work across .NET Framework (on Windows), .NET Core (on Windows, macOS, and Linux), and Xamarin (on iOS, Android, and Windows Mobile), you can do so most easily with .NET Standard.
The following table summarizes versions of .NET Standard, and which platforms they support. Note:
Platform |
1.1 |
1.2 |
1.3 |
1.4 |
1.5 |
1.6 |
2.0 |
.NET Core |
→ |
→ |
→ |
→ |
→ |
1.0, 1.1 |
vNext |
.NET Framework |
4.5 |
4.5.1 |
4.6 |
→ |
→ |
→ |
4.6.1 |
Xamarin/Mono |
→ |
→ |
→ |
→ |
→ |
4.6 |
vNext |
UWP |
→ |
→ |
→ |
10 |
→ |
→ |
vNext |
We will create a class library using .NET Standard so that it can be used cross-platform (at least in theory!).
Start Microsoft Visual Studio 2017.
In Visual Studio, press Ctrl + Shift + N or choose File | New | Project....
In the New Project dialog, in the Installed | Templates list, expand Visual C#, and then select .NET Standard. In the list at the center, select Class Library (.NET Standard), type the name Ch16_SharedLibrary
, change the location to C:Code
, type the solution name Chapter16
, and then click on OK, as shown in the following screenshot:
In Solution Explorer, expand Dependencies, SDK, and NETStandard.Library, and note the long list of packages that are included, as shown in the following screenshot:
Right-click on Ch16_SharedLibrary
and choose Edit Ch16_SharedLibrary.csproj.
Note a Class Library (.NET Standard) targets version 1.4 by default, as shown in the following markup:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard1.4</TargetFramework> </PropertyGroup> </Project>
In the Code
folder in your user folder, create a subfolder named Chapter16
, and then a sub-sub-folder named Ch16_SharedLibrary
.
Start Visual Studio Code and open the Code/Chapter16/Ch16_SharedLibrary
folder.
In Visual Studio Code, navigate to View | Integrated Terminal, and then enter the following command:
dotnet new classlib
Click on the Ch16_SharedLibrary.csproj
file and then note that a class library generated by the dotnet
CLI targets version 1.4 by default, as shown in the following markup:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard1.4</TargetFramework> </PropertyGroup> </Project>
18.118.137.7