.NET Target Framework and C# Language: A Summary

.NET Target Framework and C# Language: A Summary

·

4 min read

Intro

This article plays as a summary on 2 concepts in the .NET Ecosystem: Target Framework and C# Language.

Target Framework

Whenever you create a .NET project, either as a class library, a test suite, a web assembly, or a web API server, etc., you have to identify the target framework of that project. Examples of these frameworks:

  • .NET Framework 2.0

  • .NET Framework 4.5.2

  • .NET Framework 4.8

  • .NET Standard 2.0

  • .NET Standard 2.1

  • .NET Core 2.0

  • .NET Core 3.1

  • .NET 6

  • .NET 8

It is noteworthy that same numbering does not mean similar nor related frameworks, nor implying they can work together. For examples, .NET Framework 2.0, .NET Standard 2.0, .NET Core 2.0 are 3 different target frameworks.

Depending on the type of the project, only some Target Frameworks are compatible. For example, Blazor Server App project can have Target Framework of .NET Core 3.1, .NET 6, or .NET 8; but not any of the .NET Framework. The IDE (e.g. Visual Studio) can support presenting you the available options when you create the project.

The Target Framework also defines whether one project can reference another. For example, a .NET 6 project can reference another .NET 6, as well as all .NET Standard and .NET Core. However, it cannot reference .NET 7 or higher, or any of the .NET Framework.

Another example: .NET Framework 4.8 can reference .NET Framework 4.8 and lower, .NET Standard 2.0 and lower; but it cannot reference any .NET Core or .NET 6/7/8 or .NET Standard 2.1 versions.

So, is there any universal rule about what framework can reference what framework? Well, kind of, but you'd better look-up and do homework for that, since it is quite long to explain. Yet, here is the sum-up from my understanding:

  1. Microsoft developed the .NET Framework X.X first, which mostly work on Windows OS.

  2. Later Microsoft started a new line of frameworks: .NET Core X.X. This line of frameworks can work on wider range of OS, such as Linux, MacOS. After .NET Core 5.0, they want to make it official and simple: From that point, we have .NET 6, .NET 7, and .NET 8.

  3. So now we have 2 lines of frameworks: .NET Framework X.X and .NET (Core) X.X. A framework from one line cannot reference from the other line. And obviously, on a same line, a framework cannot reference one with higher (newer) number.

  4. To solve the problem where there can be common features of 2 projects from 2 lines without duplicating the codes, Microsoft created the 3rd line: .NET Standard. .NET Framework X.X and .NET (Core) X.X can reference a version from this 3rd line. Visit .NET Standard to look up which one can reference which.

Finally, the Target Framework defines whether a project can install a certain Nuget Package/Library, which is also a form of referencing.

C# Language

Thankfully, numbering and versioning of C# Language are simpler and linear: Just C# X.X, with no different lines of versions like the Target Frameworks.

Each Target Framework comes with a Default C# language, for examples:

  • .NET Framework 4.8: C# 7.3

  • .NET Standard 2.0: C# 7.3

  • .NET Standard 2.1: C# 8.0

  • .NET 6: C# 10

  • .NET 8: C# 12

For full list of default versions: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults

The Default version means the Highest Compatible C# Language version a certain Target Framework can have. Therefore, even if it is possible to change it out of the default one, doing so is pointless: Changing to lower version removes some benefits and features the default version has, and changing to higher version can lead to Incompatibility issues.

Therefore, the best way to get the features that are available in high versions (e.g. record from C# 10) is to start with whether it is possible to migrate your current Target Framework version to the higher one with the expected C# Language default version.

References

.NET Framework versions and dependencies

.NET and .NET Core Support Policy

.NET Standard

C# Language Versioning