Why can't I use System.Index in UWP even after enabling C#8 in the csproj?

Viewed 531

In a UWP library project (so old format instead of SDK format csproj) I added <LangVersion>8.0</LangVersion> and it lets me use C#8 features such as the alternate using statement (without braces). However, the System.Index type is not available. Why is this and how do I get it to work?

2 Answers

Why can't I use System.Index in UWP even after enabling C#8 in the csproj?

Please check langversion (C# Compiler Options) document,

C# 8.0 only available in Microsoft Visual Studio/Build Tools 2019, version 16.3, or .NET Core 3.0 SDK. And you mentioned Index only apply to NET Core 3.1 3.0. NET Standard 2.1. So it will not works in old UWP library project.

how do I get it to work?

The recommend way is you could make an .NET Standard(2.1) class lib to instead the old UWP class lib. However, NET Standard 2.1 doesn't support UWP yet. and it is TBD status, please pay a attention to the following release.

You can't. It's not a matter of Visual Studio or C# language version. If you check the Applies To section in the Index docs, you'll sees it's only available in .NET Standard 2.1 type. You can't use it in older runtimes.

One option would be to upgrade all your projects to .NET Core 3.1 or .NET Standard 2.1. Another option would be to multi-target, and add conditional compilation directives to use Index only for the .NET Standard 2.1 target

Related