.NET Standard for Absolute Beginners

If you are a beginner in ASP.NET Core chances are at some point in time you stumbled upon this - .NET Standard. You probably wondered what it is and how is it different than .NET Framework and .NET Core. Although you might have heard this term, while developing your ASP.NET Core apps you might not have bothered about it at all. This might have made you wonder even more as to what's the use of .NET Standard if it's not actively used while developing your apps. You might have also felt bit confusing about the version number that accompanies .NET Standard. This article attempts to explain all these questions about .NET Standard from beginner's perspective. My aim is to quickly enable you to grasp the concept and utility of .NET Standard and I also provide pointers for more detailed understanding.

Before even going into the details of what .NET Standard is all about, let me present a hypothetical scenario to you for the sake of setting a background and analogy.

Suppose you are building a software component that encapsulates certain business functionality. You created that component and assigned version number 1.0 to it. Then you also decided to create a help file explaining that component. You created that help file and for better management decide to give it version number 1.0. At this stage the component and the help file both have version 1.0. Now, after a few days you found out some inaccuracies in the help file and to fix them you modified the help file. The modified help file has version number 1.1. So, component is still 1.0 but the help file is 1.1. After a few days you released a new major version of the component, say 2.0. Now the component is 2.0 and the help file is 1.1. You may update the help file at a later date and change its version number to say 1.2.

Did you get the point? The component and the help file, although related, are two independent things. They evolve at their own pace and have their own version numbers.

Now let's come to the point and understand the difference between .NET Framework, .NET Core, and .NET Standard.

.NET Framework is a flavor of .NET that is available to us for over a decade and runs on Windows operating system. .NET Core was first released in 2016 and is an open source framework. .NET Framework as well as .NET Core both are software frameworks. You can download them and install them on your machine. On the other hand .NET Standard is a specification. You can compare .NET Framework and .NET Core with the software component of the example discussed earlier and .NET Standard to the help file. .NET Framework and .NET Core have their own version numbers, the latest versions being 4.8 and 3.0 respectively.

The .NET Standard is a specification of .NET APIs that are available on all .NET implementations. You can think of .NET Standard as a catalog of APIs available on all the flavors of .NET. The .NET Standard has its own version number, latest being 2.1. The .NET Standard is a cumulative collection of APIs. That means if version 1.0 has, say, 100 APIs then version 2.1. has, say, 1000 APIs. Since .NET Standard is merely a specification (and not a software) obviously it's not something you can download and install.

The .NET Framework and .NET Core implement a particular version of .NET Standard. For example, .NET Core 3.0 implements .NET Standard 2.1 and .NET Core 2.1 implements .NET Standard 2.0.

When you create a new ASP.NET Core app you typically create it with a particular version of ASP.NET Core, say 1.1, 2.1, or 3.0. In such cases you don't need to bother about .NET Standard at all. But think about this situation - Suppose you want to create a class library that will be utilized by multiple versions and flavors of .NET implementations. That means you want to write a piece of code that can be shared across multiple framework implementations. In such cases you can't target your code at a particular .NET version. That's where .NET Standard is quite useful. You can target your code at a particular version of .NET Standard and doing so will automatically ensure that it will run on all the frameworks that implement that version of .NET Standard.

Ok. Let's take a simple example and understand how all this fits in the picture.

Go to this page and scroll to the bottom to reveal this utility:

As you can see in this figure, there is a dropdown list containing various versions of the .NET Standard specification. When you pick a version in this dropdown list various .NET implementations that incorporate that version of .NET Standard are shown below. For example, .NET Standard 2.0 is implemented by .NET Core 2.0, 2.1, 2.2, and 3.0 but not by .NET Core 1.0 and 1.1. This way you can figure out all the .NET implementations on which your code can run.

Next, open this page in the browser. It's .NET API browser and you can use it find out whether a particular class, method, or property is a part of a certain version of .NET Standard.

As an example, enter System.IO.File in the textbox. And using the dropdown list set filter to .NET Standard 2.1.

Since .NET Standard 2.1 does include System.IO.File class as a part of specification, it will be displayed in the results below.

Now, change the .NET Standard version to 1.2 or below. You will find that System.IO.File is not included in those versions of the specification.

If you go to any APIs documentation page (System.IO.File class in this case) you will find this section at the bottom.

As you can see, .NET Standard versions that include the API under consideration are listed there.

Now, let's confirm this finding at code level.

Open Visual Studio 2019 and create a new project based on Standard Library project template. 

This project template is similar to the Class Library project template but it targets a .NET Standard version rather than a particular .NET implementation. Once the project is created, double click on the project in the Solution Explorer. You will find this:

As you can see, the target framework is set to netstandard2.0. This means this class library is based on .NET Standard 2.0 specification and can be used by all .NET implementation that incorporate .NET Standard 2.0 (you can find them using the tool discussed earlier).

Now open the default class from the project and write the code shown below:

public class Class1
    public void WriteToFile()
        StreamWriter writer = File.CreateText
        writer.WriteLine("Hello World!");

This code simply uses System.IO.File class to create a new text file named HelloWorld.txt at the specified location. It then writes "Hello World!" into it and closes the file.

Compile the project. Since .NET Standard 2.0 includes System.IO.File class, the code will be compiled correctly.

Now, open the project file again and change the <TargetFramework> value to netstandard1.2. Build the project again. This time you will get compilation error and File class will be shown with red underline.

This is because .NET Standard 1.2 doesn't include System.IO.File class.

I hope you got some idea as to what .NET Standard is about and how it can be used. You can read more about .NET Standard in the Microsoft's official documentation available here.

That's it for now! Keep coding!!

Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 25 November 2019

Tags : ASP.NET ASP.NET Core .NET Framework C# Visual Studio