gpt4 book ai didi

c# - 无法使用 C# 将带 [] 的索引应用于类型为 'System.Array' 的表达式

转载 作者:可可西里 更新时间:2023-11-01 07:50:16 24 4
gpt4 key购买 nike

我正在尝试使用包含字符串数组的列表,但是当我尝试使用方括号访问数组元素时,我收到错误消息。

我的数组列表是这样声明的:

public List<Array> alphabet = new List<Array>();

我还有一个像这样声明的字符串数组:

 string[] encrypted = new string[text.Length];

我可以访问一个数组,但不能访问另一个数组

string a = alphabet[1][2]; // this gives me an error

string b = encrypted[1]; // this works fine

最佳答案

错误非常简单;您不能在 Array 上使用索引器。 Array 类是所有数组类型的基类,数组隐式继承自 Array。但是,Array 本身没有索引器。这是您的错误的演示:

int[] numbers = new[] {1, 2, 3, 4, 5};

numbers[2] = 11; // Okay

Array arr = numbers as Array;

arr[2] = 11; // ERROR!

因此,如果您想使用索引器,请将您的元素类型更改为数组,例如:

public List<string[]> alphabet = new List<string[]>();

关于c# - 无法使用 C# 将带 [] 的索引应用于类型为 'System.Array' 的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com