I created a small Record type in C#:
However the compiler didn’t like it:
Why? Because I was adding this code to a .NET Standard 2.0 library that doesn’t support C# 9 features.
As a reminder, a small overview:
Target framework Version | C# Language Version |
.NET 5 | C# 9.0 |
.NET Core 3.x | C# 8.0 |
.NET Core 2.x | C# 7.3 |
.NET Standard 2.1 | C# 8.0 |
.NET Standard 2.0 | C# 7.3 |
I didn’t want to upgrade to .NET 5 so what are my options? Turns out, that you can work around this with a small compiler hack…
Let’s try it:
- First you need to override the language version in the csproj file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<PropertyGroup> | |
<LangVersion>9.0</LangVersion> | |
</PropertyGroup> |
- Then you should add the following code to get rid of the compiler error:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace System.Runtime.CompilerServices | |
{ | |
public class IsExternalInit { } | |
} |