c# - 为什么这会导致 'Index out of range' 异常?

标签 c# arrays exception

    public void button2_Click(object sender, EventArgs e)

    {
        char[] delimiters = { ',', '\r', '\n', ' ' };
        string[] content = File.ReadAllText(CSV_File).Split(delimiters);

        int bounds = (content.GetUpperBound(0)); //bounds of this content string is 96
        int i = 1;
        id = new string[(bounds / 4)]; //for example index size = 96 / 4 = 24

        for (i = 0; i <= (bounds / 4); i++)
        {
            int rows = (i * 4); // gets every 4th value
            id[i] = content[rows]; //inserts row 96 into id 24 - fails here
        }
    }

现在坚持了一段时间。确切的错误是“索引超出了数组的范围”。但是我不知道这是指哪个索引。

最佳答案

更改 <=<在 for 循环中。

关于c# - 为什么这会导致 'Index out of range' 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958521/

相关文章:

c# - 如何在 C# WebBrowser 控件中获取重定向的 url

JavaScript - 搜索数组中的第一个字符

exception - 是否存在未对齐数据会导致 x86 CPU 产生异常的情况?

c++ - 向用户显示异常调试信息

c# - 延迟函数中的 System.IO.FileNotFoundException

c# - 创建多个文本文件并写入

c# - protected 通用类 - 是否受支持?

c# - e.Row.findcontrol() 在数据绑定(bind)后返回 null 但所有数据绑定(bind)字段都正常工作

创建 2D 阵列板

java - while 循环在 for 循环中,困惑吗?