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

标签 c# arrays string list

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

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

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/

相关文章:

c# - BCL隐藏拳击?

php - 如何在一个表中打印 3 个数组,每个数组应代表 1 列?

javascript - JavaScript 中 includes 方法的时间复杂度

java - 为什么我的数组不满足该 if 语句的条件?

c - strcat 的问题

java - Android 的奇怪问题,程序可以编译但无法运行

c++ - 读取未知长度的数字

c# - 使用 MVC ViewContext 渲染 View 时指定样式表媒体

c# - 将类型转换为 DBSet<>

c# - MongoDB C# 从 ID 列表中获取所有文档