Array IndexOutofBoundsException เกิดขึ้นใน Java เทียบเท่าใน C # คือ IndexOutOfRangeException
IndexOutOfRangeException เกิดขึ้นเมื่อดัชนีอยู่นอกขอบเขตของอาร์เรย์
ตัวอย่าง
using System;
using System.IO;
using System.Collections.Generic;
namespace Demo {
class Program {
static void Main(string[] args) {
int[] arr = new int[3];
arr[0] = 5;
arr[1] = 7;
arr[2] = 8;
arr[4] = 24; // this shows an error
}
}
} ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ มันแสดงข้อผิดพลาดต่อไปนี้ -
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.