【问题标题】:System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' in xamarin AppSystem.IndexOutOfRangeException: '索引超出了数组的范围。'在 xamarin 应用程序中
【发布时间】:2020-03-20 17:42:09
【问题描述】:

我对此有疑问。我正在尝试生成一个包含许多符号的列表(每个符号需要 2 个)。有时有效,有时会抛出此错误:

System.IndexOutOfRangeException: '索引超出了数组的范围。'

代码:

static int[] letras = new int[16];

string[] simbolo = new string[]{"A","B","A","B","C","D","C","D","E","F","E","F","G","H","G","H"};

for (int i = 0; i < letras.Length; i++){
    letras[i] = Convert.ToString(simbolo)[alea.Next(0, simbolo.Length)];
}

这是一个 Xamarin C# 应用程序...

【问题讨论】:

  • 提示:你认为Convert.ToString(simbolo) 的结果是什么?我怀疑你会感到惊讶。

标签: c# xamarin


【解决方案1】:

正如 Jon Skeet 所说。

Convert.ToString(simbolo) 实际上会将 simbolo 的类型转换为字符串,即 - System.String[] 不是您想要的实际字符串。

如下更改您的代码 -

static int[] letras = new int[16];

string[] simbolo = new string[]{"A","B","A","B","C","D","C","D","E","F","E","F","G","H","G","H"};

for (int i = 0; i < letras.Length; i++){
    letras[i] = string.Join("", simbolo);[alea.Next(0, simbolo.Length)];
}

【讨论】:

    猜你喜欢
    • 2022-11-14
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多