While creating a new NuGet package this week I stumbled upon the TargetFramework property in my .csproj file, and thought by myself, should I use netstandard2.1, netstandard2.0, net5.0, net6.0 or net7.0. Or even a combination of them using the TargetFrameworks (plural) property of the .csproj file.
In the past I have always used netstandard2.1, but this left me thinking if in 2023 this was still the right way to go.
Target the latest LTS version of .NET (currently .NET 6) or the latest version (.NET 7) for new projects in 2023.
.NET Standard played a critical role in unifying the .NET ecosystem, but its focus has shifted with the release of .NET 5. Existing .NET Standard 2.1 code doesn't need to be changed, as .NET 6+ supports it.
What is .NET Standard All About?
Before diving into the current state of .NET Standard, it's essential to understand its purpose when it was created. .NET Standard is a set of APIs that all .NET implementations must support.
It serves as a foundation for building libraries and applications across multiple platforms and devices. This allows developers to write code that works on multiple platforms and devices, reducing the need for platform-specific code. As a result, the .NET ecosystem became more consistent and interoperable, making it easier for developers to share and reuse code across projects.
.NET Standard history
Looking at the timeline of the history of .NET Framework, .NET (Core), and .NET Standard, you can see that .NET Standard emerged around the same time .NET Core 1.0 was released. This was a logical evolution of the ecosystem, as developers needed to be able to target multiple targets (.NET Framework & .NET Core) using a single set of APIs across multiple platforms and devices.
At that time, there were three major flavors of .NET:
- .NET Framework: containing WPF, Windows Forms, and ASP.NET apps
- .NET Core: containing UWP and ASP.NET Core applications
- Xamarin: container iOS, OS X, and Android apps
This meant that you had to master three different base class libraries to write code that works across all of them.
That's where .NET Standard came in. For developers, this meant they only had to master one base class library. Libraries targeting .NET Standard would be able to run on all .NET platforms. And platform providers didn't have to guess which APIs they needed to offer to consume the libraries available on NuGet. They could just implement .NET Standard.
The Evolution of .NET Standard
.NET Standard has undergone several revisions since its inception. The latest version, .NET Standard 2.1, introduced many new features and improvements, such as additional APIs and enhanced performance.
.NET Standard 2.1 stopped .NET Framework support
It's important to note that .NET Standard 2.1 is the first version not supported by .NET Framework, which means that libraries targeting .NET Standard 2.1 will not work with .NET Framework applications.
What Changed in .NET 5
In 2020, Microsoft released .NET 5, which aimed to unify the .NET ecosystem by bringing together the best features of .NET Framework, .NET Core, and Xamarin. This new version marked the end of .NET Standard's evolution, as future .NET versions would only focus on extending the capabilities of .NET 5 and its successors.
With .NET 5, developers no longer need to target a specific .NET Standard version, as they can target the latest .NET version.
This allows for a more streamlined development experience, as libraries and applications can access the entire .NET API surface without restrictions.
What to Target in 2023
Given that .NET 5 support ended on May 10, 2022, we should always target a newer framework that is still supported. Today, this would be .NET 6 (LTS) or .NET 7. Developers should focus on targeting at least the latest LTS version of .NET for their projects or support the latest STS version. This will ensure access to the most comprehensive set of APIs and features while also providing the best performance and backward compatibility across platforms and devices.
For existing code that targets .NET Standard 2.1, there's no need to change the TargetFramework to .NET 6 or higher, as .NET 6 implements .NET Standard 2.1 and earlier. The only reason to retarget from .NET Standard to .NET 6+ would be to gain access to more runtime features, language features, or APIs.
Target Framework Monikers (TFM)
So when creating a new project for a NuGet package, your .csproj could use any of the following TFM's:
|Target framework moniker (TFM)
|Implemented .NET Standard version
You specify the desired TFM in the .csproj file. For example, to target .NET 7, you would use the following .csproj file:
The Future of .NET Standard
Although .NET Standard will no longer evolve, it remains a crucial part of the .NET ecosystem. Existing libraries and applications targeting .NET Standard will continue to be supported, ensuring that developers can maintain and update their projects as needed. As new .NET versions emerge, they will maintain compatibility with .NET Standard, ensuring a smooth transition for projects that choose to migrate.
In conclusion, .NET Standard has played a critical role in unifying the .NET ecosystem, but its focus has shifted with the release of .NET 5. By targeting the latest .NET version, developers can access the full range of APIs and features, while ensuring optimal compatibility and performance across platforms and devices.