Sharing code cross-platform with .NET Standard class libraries

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:

  • .NET Core and Xamarin support .NET Standard 1.6
  • .NET Framework 4.6.1 already supports .NET Standard 2.0, but does not support .NET Standard 1.6
  • We must wait for a future release of .NET Core and Xamarin before all platforms are synchronized and support .NET Standard 2.0

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

Creating a .NET Standard class library

We will create a class library using .NET Standard so that it can be used cross-platform (at least in theory!).

Using Visual Studio 2017

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:

Using Visual Studio 2017

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:

Using Visual Studio 2017

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> 

Using Visual Studio Code on macOS

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> 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.7