Introduction to .NET Standard

What is .NET Standard?

.NET Standard is a formal specification of .NET APIs that are intended to be available on all .NET implementations. It aims to establish greater uniformity in the .NET ecosystem by defining a set of APIs that all .NET platforms must implement, thereby ensuring that code written for one platform can run on others without modification.

The primary goal of .NET Standard is to enable code sharing across different .NET platforms, such as .NET Core, .NET Framework, Xamarin, and others. By targeting the .NET Standard, developers can create libraries that are compatible with multiple platforms, reducing code duplication and easing the maintenance burden.

Why .NET Standard?

Before the .NET Standard, code sharing between different .NET platforms was challenging due to the inconsistencies in the APIs available on each platform. Developers had to use Portable Class Libraries (PCLs), which required careful selection of target platforms and often resulted in limited API availability.

.NET Standard addresses these issues by providing a single, unified API surface that all platforms must support. This ensures a consistent development experience and simplifies the process of building cross-platform libraries.

.NET Standard Versions

.NET Standard has multiple versions, each incrementally adding more APIs. Higher versions include all APIs from the previous versions and add new ones. Here’s a brief overview of some key versions:

  • .NET Standard 1.0: The first version, with a minimal set of APIs for basic functionality.
  • .NET Standard 1.1 - 1.6: Incremental additions of APIs, gradually increasing compatibility with more .NET Framework libraries.
  • .NET Standard 2.0: A major version that significantly expanded the API surface, making it much easier to port code from the .NET Framework. It includes over 32,000 APIs and added compatibility with existing .NET Framework libraries.
  • .NET Standard 2.1: Adds even more APIs and introduces support for new .NET Core features, but it is not supported on .NET Framework, which means it is intended for modern .NET platforms like .NET Core and Xamarin.

Benefits of .NET Standard

  1. Code Sharing: By targeting .NET Standard, developers can create libraries that work across multiple platforms, including .NET Core, .NET Framework, Xamarin, and others. This reduces the need for platform-specific code and simplifies the development process.
  2. Simplified Library Development: Developers can write and maintain a single version of a library that can be used on various platforms, eliminating the need for separate codebases. This also means fewer bugs and easier maintenance.
  3. Consistent API Surface: .NET Standard ensures a consistent set of APIs across all platforms, providing a uniform development experience. This helps developers to write code that is more predictable and less prone to platform-specific issues.
  4. Future-Proofing: Targeting .NET Standard helps future-proof your libraries, as it ensures compatibility with new .NET platforms and versions. This is particularly important as the .NET ecosystem continues to evolve.

How to target .NET standards?

To target .NET Standard in your projects, you need to specify the desired .NET Standard version in your project file. Here’s an example of how to do this in a .NET Standard class library project.

Create a new .NET Standard library project.

dotnet new classlib -n MyNetStandardLibrary

Open the project file (MyNetStandardLibrary.csproj) and specify the target framework.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

Add code to your library as usual.

using System;
namespace MyNetStandardLibrary
{
    public class Class1
    {
        public string GetGreeting(string name)
        {
            return $"Hello, {name}!";
        }
    }
}

Build and use your library.

dotnet build

You can now reference this library in other projects targeting .NET Core, .NET Framework, Xamarin, and more.

Summary

.NET Standard is a powerful tool for ensuring code compatibility and promoting code sharing across different .NET platforms. By targeting .NET Standard, developers can create libraries that are versatile, maintainable, and future-proof. Whether you are building new libraries or porting existing ones, .NET Standard provides a unified API surface that simplifies cross-platform development and helps you get the most out of the .NET ecosystem.


Similar Articles