错误:未定义或导入预定义类型“System.Range”

Error: Predefined type 'System.Range' is not defined or imported

提问人:hellouniverse 提问时间:9/21/2020 最后编辑:hellouniverse 更新时间:10/9/2023 访问量:2825

问:

早些时候我收到错误:

Feature 'range operator' is not available in C# 7.3. Please use language version 8.0 or greater.

当我升级项目时,以下错误仍然存在:

Predefined type 'System.Range' is not defined or imported

基本上尝试测试我们如何检索数组或字符串的片段:

using System;
using System.Windows.Forms;

namespace TestRange
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var story = "Testing C# 8";
            Console.WriteLine(story[^6..^0]); // (last 6 chars)
        }
    }
}

我引用但没有帮助的文章:

https://learn.microsoft.com/en-us/dotnet/api/system.range?view=netcore-3.1 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes

https://www.infoworld.com/article/3532284/how-to-use-indices-and-ranges-in-csharp-80.html

https://www.codejourney.net/2019/02/csharp-8-slicing-indexes-ranges/

一个相关问题:C# 中的数组切片

C# .NET 范围 切片

评论

0赞 Hayden 9/21/2020
您是否能够提供项目属性的屏幕截图。
0赞 hellouniverse 9/21/2020
屏幕截图1
0赞 hellouniverse 9/21/2020
屏幕截图2
0赞 Hayden 9/21/2020
C# 8 仅在 .Net Core 3 及更高版本中受支持。
1赞 Brett Caswell 9/21/2020
System.Range 和 System.Index 不会导入用于此项目目标。Netframework,即使您确实将 LangVersion 添加到了项目文件中。但是,处理该问题是“实施”规范的问题;因此,实现它(并缓解导入的问题)的一种方法是通过下载\including netstandard2.0 和 netstandard2.0 的 IndexRange(第三方)nuget 包。NetFramework c#8 中。

答:

0赞 sunnamed 10/9/2023 #1

作为 .NET Framework 461 或 smth 的解决方案,可以在 csproj 中使用此 NuGet 包:

<ItemGroup>
  <PackageReference Include="Contrib.Bcl.Ranges" Version="1.0.0"/>
</ItemGroup>