There are several great ways to share code between applications and to create libraries to share with the world. With more and more platforms supporting C# and .NET, a new type of library, which we call .NET Standard libraries, was created to simplify development for all developers.
You can think of .NET Standard as a bunch of APIs you can use in a library that can be shared across several different platforms. In fact, that’s actually a formal specification of .NET APIs that are intended to be available on all .NET implementations.
John Montgomery recently announced the general availability of .NET Core 2.0, Visual Studio 2017 version 15.3, and Visual Studio for Mac version 7.1, which include support for .NET Standard 2.0. What’s awesome about .NET Standard 2.0 is that it adds over 20,000 new APIs for developers, compatibility with the top 70% of NuGet packages, and adds the addition of UWP to the supported platforms with the Fall Creators Update.
For Xamarin developers, we’re happy to announce preview support for .NET Standard 2.0 libraries with Xamarin.iOS 10.14, Xamarin.Android 7.5, Xamarin.Mac 3.8, and Mono 5.4, which you can get your hands on today!
Getting Started: Visual Studio 2017
You’ll need to get the the version 15.4 preview to get start with Visual Studio 2017. Simply download and install the Visual Studio 2017 Preview with the Mobile .NET workload. After the update is finished, you’ll need to install .NET Core 2.0, which is a quick and easy install.
Now, we can create a new project and select the .NET Standard Library:
By default the library will target .NET Standard 2.0. However, this can always be toggled in the projects properties:
Getting Started: Visual Studio for Mac
To get started with .NET Standard libraries with Visual Studio for Mac, you’ll need to upgrade to Visual Studio for Mac version 7.2, which is now available in the Alpha channel. Then, simply install the new release of .NET Core 2.0 for macOS.
We’ll find the new .NET Standard Library under Multiplatform -> Library:
Creating one brings us to a selection of which version of .NET Standard the library should use:
That’s it! This new library can now be consumed by any platform that supports .NET Standard 2.0!
Which Do I Choose?
When choosing a .NET Standard version, you should consider this trade-off:
- The higher the version, the more APIs are available to you.
- The lower the version, the more platforms implement it.
With the introduction of .NET Standard 2.0 the decision has been simplified as nearly all platforms support it including .NET Core 2.0, .NET Framework 4.6.1, Mono, Xamarin.iOS, Xamarin.Android, Xamarin.Mac, and UWP.
To learn more about .NET Standard, be sure to head over to the amazing .NET Standard documentation that will walk you through everything you need to know. Better yet, check out the latest On .NET show on Channel 9, where Immo Landwerth and Rich Lander do a deep dive on .NET Standard: