Setup PolySharp to use modern C# features on older runtimes
mainPolySharp allows you to use modern C# language features (like nullability annotations, pattern matching, etc.) on older runtimes such as .NET Framework or UWP. It works as a source generator that automatically detects and generates the necessary 'magic types' required by the compiler.
To use PolySharp, follow these two steps:
- Add a reference to the PolySharp NuGet package to your project.
- Set your C# language version to the latest (or your desired version) by updating the
<LangVersion>MSBuild property in your.csprojfile.
Limitations:
- Features requiring actual runtime support (e.g.,
static abstract members) cannot be polyfilled and will not work. - On .NET Framework 3.5, usage is limited.
System.Rangerequires a reference toSystem.ValueTupleto be generated, and features likerecordsmay not work due to missing compiler APIs.
<PropertyGroup>
<LangVersion>14.0</LangVersion>
</PropertyGroup>