指数是数组c#的边界之外 [英] Index was outside the bounds of the array c#

查看:197
本文介绍了指数是数组c#的边界之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组尺寸[5] 。我加了5个值到这个数组,然后删除的值。现在我的阵列只能有一个值。如果我的另一个值添加到这个数组它显示的错误
指数数组的边界之外。

I have an array with size[5]. I added 5 values into this array and then removed the values. Now my array have only one value. If I add another value to this array it shows the error Index was outside the bounds of the array.

推荐答案

如果您有大小5的阵列可以添加值0-4(数组索引0开始):

If you have an array of size 5 you can add values 0 to 4 (arrays start at index 0):

object[] arr = new object[5];
arr[0] = new object();
arr[4] = new object();

// this will give your Index was outside the bounds of the array exception:
arr[5] = new object();

这篇关于指数是数组c#的边界之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆