无法使用C#将索引与[]应用于类型'System.Array'的表达式 [英] Cannot apply indexing with [] to an expression of type 'System.Array' with C#
问题描述
我正在尝试使用包含C#中的字符串数组的列表,但我不知道如何正确格式化我的声明.
I'm trying to use a List containing string arrays in C# but I don't know how to format my declaration properly.
private String encrypt(char[] text)
{
Random rnd = new Random();
string[] encrypted = new string[text.Length];
for (int i = 0; i < text.Length; i++)
{
int symnumb = rnd.Next(listmin, listmax);
encrypted[i] = alphabet[getnumber(text[i])][symnumb].ToString();
}
return string.Join("", encrypted);
}
这是更深层次的:
private int getnumber(char letter)
{
for (int i = 0; i < 27; i++)
{
if (letter == alphabetc[i])
{
return i;
}
}
return -1;
}
我必须发布许多无关的代码,但是已加密"是字符串数组,字母"是包含字符串的数组列表.
I have to much irrelevant code to post it all, but "encrypted" is a string array, "alphabet" is a list of arrays containing strings.
声明:
public List<Array> alphabet = new List<Array>();
public char[] alphabetc = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' '};
任何帮助都会很棒.
推荐答案
错误非常简单;您不能在Array
上使用索引器. Array
类是所有数组类型的基类,并且数组是隐式继承自Array.但是,Array
本身没有索引器.这是您的错误的证明:
The Error is pretty straightforward; you can't use an indexer on an Array
. Array
class is a base class for all array types, and arrays are implicitly inherit from Array. But, Array
itself doesn't have an indexer. Here is a demonstration of your error:
int[] numbers = new[] {1, 2, 3, 4, 5};
numbers[2] = 11; // Okay
Array arr = numbers as Array;
arr[2] = 11; // ERROR!
因此,如果要使用索引器,请将元素类型更改为某些数组,例如:
So if you want to use the indexer, change your element type to an array of something for example:
public List<string[]> alphabet = new List<string[]>();
这篇关于无法使用C#将索引与[]应用于类型'System.Array'的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!