IndexOutOfRangeException Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Pengecualian yang dilemparkan ketika upaya dilakukan untuk mengakses elemen array atau koleksi dengan indeks yang berada di luar batasnya.
public ref class IndexOutOfRangeException sealed : Exception
public ref class IndexOutOfRangeException sealed : SystemException
public sealed class IndexOutOfRangeException : Exception
public sealed class IndexOutOfRangeException : SystemException
[System.Serializable]
public sealed class IndexOutOfRangeException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class IndexOutOfRangeException : SystemException
type IndexOutOfRangeException = class
inherit Exception
type IndexOutOfRangeException = class
inherit SystemException
[<System.Serializable>]
type IndexOutOfRangeException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type IndexOutOfRangeException = class
inherit SystemException
Public NotInheritable Class IndexOutOfRangeException
Inherits Exception
Public NotInheritable Class IndexOutOfRangeException
Inherits SystemException
- Warisan
- Warisan
- Atribut
Keterangan
Pengecualian IndexOutOfRangeException dilemparkan ketika indeks yang tidak valid digunakan untuk mengakses anggota array atau koleksi, atau untuk membaca atau menulis dari lokasi tertentu dalam buffer. Pengecualian ini mewarisi dari Exception kelas tetapi tidak menambahkan anggota unik.
Biasanya, IndexOutOfRangeException pengecualian dilemparkan sebagai akibat dari kesalahan pengembang. Alih-alih menangani pengecualian, Anda harus mendiagnosis penyebab kesalahan dan memperbaiki kode Anda. Penyebab kesalahan yang paling umum adalah:
Lupa bahwa batas atas koleksi atau array berbasis nol kurang dari jumlah anggota atau elemennya, seperti yang diilustrasikan contoh berikut.
using System; using System.Collections.Generic; public class Example { public static void Main() { List<Char> characters = new List<Char>(); characters.InsertRange(0, new Char[] { 'a', 'b', 'c', 'd', 'e', 'f' } ); for (int ctr = 0; ctr <= characters.Count; ctr++) Console.Write("'{0}' ", characters[ctr]); } } // The example displays the following output: // 'a' 'b' 'c' 'd' 'e' 'f' // Unhandled Exception: // System.ArgumentOutOfRangeException: // Index was out of range. Must be non-negative and less than the size of the collection. // Parameter name: index // at Example.Main()let characters = ResizeArray() characters.InsertRange(0, [| 'a'; 'b'; 'c'; 'd'; 'e'; 'f' |]) for i = 0 to characters.Count do printf $"'{characters[i]}' " // The example displays the following output: // 'a' 'b' 'c' 'd' 'e' 'f' // Unhandled Exception: // System.ArgumentOutOfRangeException: // Index was out of range. Must be non-negative and less than the size of the collection. // Parameter name: index // at <StartupCode$fs>.main@()Imports System.Collections.Generic Module Example Public Sub Main() Dim characters As New List(Of Char)() characters.InsertRange(0, { "a"c, "b"c, "c"c, "d"c, "e"c, "f"c} ) For ctr As Integer = 0 To characters.Count Console.Write("'{0}' ", characters(ctr)) Next End Sub End Module ' The example displays the following output: ' 'a' 'b' 'c' 'd' 'e' 'f' ' Unhandled Exception: ' System.ArgumentOutOfRangeException: ' Index was out of range. Must be non-negative and less than the size of the collection. ' Parameter name: index ' at System.Collections.Generic.List`1.get_Item(Int32 index) ' at Example.Main()Untuk memperbaiki kesalahan, Anda dapat menggunakan kode seperti berikut ini.
using System; using System.Collections.Generic; public class Example { public static void Main() { List<Char> characters = new List<Char>(); characters.InsertRange(0, new Char[] { 'a', 'b', 'c', 'd', 'e', 'f' } ); for (int ctr = 0; ctr < characters.Count; ctr++) Console.Write("'{0}' ", characters[ctr]); } } // The example displays the following output: // 'a' 'b' 'c' 'd' 'e' 'f'let characters = ResizeArray() characters.InsertRange(0, [| 'a'; 'b'; 'c'; 'd'; 'e'; 'f' |]) for i = 0 to characters.Count - 1 do printf $"'{characters[i]}' " // The example displays the following output: // 'a' 'b' 'c' 'd' 'e' 'f'Imports System.Collections.Generic Module Example Public Sub Main() Dim characters As New List(Of Char)() characters.InsertRange(0, { "a"c, "b"c, "c"c, "d"c, "e"c, "f"c} ) For ctr As Integer = 0 To characters.Count - 1 Console.Write("'{0}' ", characters(ctr)) Next End Sub End Module ' The example displays the following output: ' 'a' 'b' 'c' 'd' 'e' 'f'Secara bergantian, alih-alih melakukan iterasi semua elemen dalam array menurut indeksnya, Anda dapat menggunakan
foreachpernyataan (dalam C#),for...inpernyataan (dalam F#), atauFor Eachpernyataan (di Visual Basic).Mencoba menetapkan elemen array ke array lain yang belum berdimensi secara memadai dan memiliki lebih sedikit elemen daripada array asli. Contoh berikut mencoba menetapkan elemen terakhir dalam
value1array ke elemen yang sama dalamvalue2array. Namun,value2array telah salah dimensi untuk memiliki enam alih-alih tujuh elemen. Akibatnya, tugas melempar IndexOutOfRangeException pengecualian.public class Example { public static void Main() { int[] values1 = { 3, 6, 9, 12, 15, 18, 21 }; int[] values2 = new int[6]; // Assign last element of the array to the new array. values2[values1.Length - 1] = values1[values1.Length - 1]; } } // The example displays the following output: // Unhandled Exception: // System.IndexOutOfRangeException: // Index was outside the bounds of the array. // at Example.Main()let values1 = [| 3; 6; 9; 12; 15; 18; 21 |] let values2 = Array.zeroCreate<int> 6 // Assign last element of the array to the new array. values2[values1.Length - 1] <- values1[values1.Length - 1]; // The example displays the following output: // Unhandled Exception: // System.IndexOutOfRangeException: // Index was outside the bounds of the array. // at <StartupCode$fs>.main@()Module Example Public Sub Main() Dim values1() As Integer = { 3, 6, 9, 12, 15, 18, 21 } Dim values2(5) As Integer ' Assign last element of the array to the new array. values2(values1.Length - 1) = values1(values1.Length - 1) End Sub End Module ' The example displays the following output: ' Unhandled Exception: ' System.IndexOutOfRangeException: ' Index was outside the bounds of the array. ' at Example.Main()Menggunakan nilai yang dikembalikan oleh metode pencarian untuk mengulangi sebagian array atau koleksi yang dimulai pada posisi indeks tertentu. Jika Anda lupa memeriksa apakah operasi pencarian menemukan kecocokan, runtime melempar pengecualian, seperti yang IndexOutOfRangeException ditunjukkan dalam contoh ini.
using System; using System.Collections.Generic; public class Example { static List<int> numbers = new List<int>(); public static void Main() { int startValue; string[] args = Environment.GetCommandLineArgs(); if (args.Length < 2) startValue = 2; else if (!Int32.TryParse(args[1], out startValue)) startValue = 2; ShowValues(startValue); } private static void ShowValues(int startValue) { // Create a collection with numeric values. if (numbers.Count == 0) numbers.AddRange( new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} ); // Get the index of a startValue. Console.WriteLine("Displaying values greater than or equal to {0}:", startValue); int startIndex = numbers.IndexOf(startValue); // Display all numbers from startIndex on. for (int ctr = startIndex; ctr < numbers.Count; ctr++) Console.Write(" {0}", numbers[ctr]); } } // The example displays the following output if the user supplies // 7 as a command-line parameter: // Displaying values greater than or equal to 7: // // Unhandled Exception: System.ArgumentOutOfRangeException: // Index was out of range. Must be non-negative and less than the size of the collection. // Parameter name: index // at System.Collections.Generic.List`1.get_Item(Int32 index) // at Example.ShowValues(Int32 startValue) // at Example.Main()open System let numbers = ResizeArray() let showValues startValue = // Create a collection with numeric values. if numbers.Count = 0 then numbers.AddRange [| 2..2..22 |] // Get the index of a startValue. printfn $"Displaying values greater than or equal to {startValue}:" let startIndex = numbers.IndexOf startValue // Display all numbers from startIndex on. for i = startIndex to numbers.Count - 1 do printf $" {numbers[i]}" let startValue = let args = Environment.GetCommandLineArgs() if args.Length < 2 then 2 else match Int32.TryParse args[1] with | true, v -> v | _ -> 2 showValues startValue // The example displays the following output if the user supplies // 7 as a command-line parameter: // Displaying values greater than or equal to 7: // // Unhandled Exception: System.ArgumentOutOfRangeException: // Index was out of range. Must be non-negative and less than the size of the collection. // Parameter name: index // at System.Collections.Generic.List`1.get_Item(Int32 index) // at Example.ShowValues(Int32 startValue) // at <StartupCode$fs>.main@()Imports System.Collections.Generic Module Example Dim numbers As New List(Of Integer) Public Sub Main() Dim startValue As Integer Dim args() As String = Environment.GetCommandLineArgs() If args.Length < 2 Then startValue = 2 Else If Not Int32.TryParse(args(1), startValue) Then startValue = 2 End If End If ShowValues(startValue) End Sub Private Sub ShowValues(startValue As Integer) ' Create a collection with numeric values. If numbers.Count = 0 Then numbers.AddRange( { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} ) End If ' Get the index of a particular number, in this case 7. Console.WriteLine("Displaying values greater than or equal to {0}:", startValue) Dim startIndex As Integer = numbers.IndexOf(startValue) ' Display all numbers from startIndex on. For ctr As Integer = startIndex To numbers.Count - 1 Console.Write(" {0}", numbers(ctr)) Next End Sub End Module ' The example displays the following output if the user supplies ' 7 as a command-line parameter: ' Displaying values greater than or equal to 7: ' ' Unhandled Exception: System.ArgumentOutOfRangeException: ' Index was out of range. Must be non-negative and less than the size of the collection. ' Parameter name: index ' at System.Collections.Generic.List`1.get_Item(Int32 index) ' at Example.ShowValues(Int32 startValue) ' at Example.Main()Dalam hal ini, List<T>.IndexOf metode mengembalikan -1, yang merupakan nilai indeks yang tidak valid, ketika gagal menemukan kecocokan. Untuk memperbaiki kesalahan ini, periksa nilai pengembalian metode pencarian sebelum melakukan iterasi array, seperti yang ditunjukkan dalam contoh ini.
using System; using System.Collections.Generic; public class Example { static List<int> numbers = new List<int>(); public static void Main() { int startValue; string[] args = Environment.GetCommandLineArgs(); if (args.Length < 2) startValue = 2; else if (!Int32.TryParse(args[1], out startValue)) startValue = 2; ShowValues(startValue); } private static void ShowValues(int startValue) { // Create a collection with numeric values. if (numbers.Count == 0) numbers.AddRange( new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} ); // Get the index of startValue. int startIndex = numbers.IndexOf(startValue); if (startIndex < 0) { Console.WriteLine("Unable to find {0} in the collection.", startValue); } else { // Display all numbers from startIndex on. Console.WriteLine("Displaying values greater than or equal to {0}:", startValue); for (int ctr = startIndex; ctr < numbers.Count; ctr++) Console.Write(" {0}", numbers[ctr]); } } } // The example displays the following output if the user supplies // 7 as a command-line parameter: // Unable to find 7 in the collection.open System open System.Collections.Generic let numbers = new List<int>() let showValues startValue = // Create a collection with numeric values. if numbers.Count = 0 then numbers.AddRange [| 2..2..22 |] // Get the index of startValue. let startIndex = numbers.IndexOf startValue if startIndex < 0 then printfn $"Unable to find {startValue} in the collection." else // Display all numbers from startIndex on. printfn $"Displaying values greater than or equal to {startValue}:" for i = startIndex to numbers.Count - 1 do printf $" {numbers[i]}" let startValue = let args = Environment.GetCommandLineArgs() if args.Length < 2 then 2 else match Int32.TryParse args[1] with | true, v -> v | _ -> 2 showValues startValue // The example displays the following output if the user supplies // 7 as a command-line parameter: // Unable to find 7 in the collection.Imports System.Collections.Generic Module Example Dim numbers As New List(Of Integer) Public Sub Main() Dim startValue As Integer Dim args() As String = Environment.GetCommandLineArgs() If args.Length < 2 Then startValue = 2 Else If Not Int32.TryParse(args(1), startValue) Then startValue = 2 End If End If ShowValues(startValue) End Sub Private Sub ShowValues(startValue As Integer) ' Create a collection with numeric values. If numbers.Count = 0 Then numbers.AddRange( { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} ) End If ' Get the index of startValue. Dim startIndex As Integer = numbers.IndexOf(startValue) If startIndex < 0 Then Console.WriteLine("Unable to find {0} in the collection.", startValue) Else ' Display all numbers from startIndex on. Console.WriteLine("Displaying values greater than or equal to {0}:", startValue) For ctr As Integer = startIndex To numbers.Count - 1 Console.Write(" {0}", numbers(ctr)) Next End If End Sub End Module ' The example displays the following output if the user supplies ' Unable to find 7 in the collection.Mencoba menggunakan atau menghitung kumpulan hasil, koleksi, atau array yang dikembalikan oleh kueri tanpa menguji apakah objek yang dikembalikan memiliki data yang valid.
Menggunakan nilai komputasi untuk menentukan indeks awal, indeks akhir, atau jumlah item yang akan diulang. Jika hasil komputasi tidak terduga, itu mungkin mengakibatkan IndexOutOfRangeException pengecualian. Anda harus memeriksa logika program Anda dalam menghitung nilai indeks dan memvalidasi nilai sebelum melakukan iterasi array atau koleksi. Kondisi berikut harus benar; jika tidak, IndexOutOfRangeException pengecualian dilemparkan:
Indeks awal harus lebih besar dari atau sama dengan Array.GetLowerBound untuk dimensi array yang ingin Anda iterasi, atau lebih besar dari atau sama dengan 0 untuk koleksi.
Indeks akhir tidak boleh melebihi Array.GetUpperBound dimensi array yang ingin Anda iterasi, atau tidak boleh lebih besar dari atau sama dengan
Countproperti koleksi.Persamaan berikut harus benar untuk dimensi array yang ingin Anda iterasi:
start_index >= lower_bound And start_index + items_to_iterate - 1 <= upper_boundUntuk koleksi, persamaan berikut harus benar:
start_index >= 0 And start_index + items_to_iterate <= CountTip
Indeks awal array atau koleksi tidak pernah bisa menjadi angka negatif.
Dengan asumsi bahwa array harus berbasis nol. Array yang bukan berbasis nol dapat dibuat oleh Array.CreateInstance(Type, Int32[], Int32[]) metode dan dapat dikembalikan oleh interop COM, meskipun tidak sesuai dengan CLS. Contoh berikut mengilustrasikan IndexOutOfRangeException yang dilemparkan saat Anda mencoba melakukan iterasi array berbasis non-nol yang dibuat oleh Array.CreateInstance(Type, Int32[], Int32[]) metode .
using System; public class Example { public static void Main() { Array values = Array.CreateInstance(typeof(int), new int[] { 10 }, new int[] { 1 }); int value = 2; // Assign values. for (int ctr = 0; ctr < values.Length; ctr++) { values.SetValue(value, ctr); value *= 2; } // Display values. for (int ctr = 0; ctr < values.Length; ctr++) Console.Write("{0} ", values.GetValue(ctr)); } } // The example displays the following output: // Unhandled Exception: // System.IndexOutOfRangeException: Index was outside the bounds of the array. // at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices) // at System.Array.SetValue(Object value, Int32 index) // at Example.Main()open System let values = Array.CreateInstance(typeof<int>, [| 10 |], [| 1 |]) let mutable value = 2 // Assign values. for i = 0 to values.Length - 1 do values.SetValue(value, i) value <- value * 2 // Display values. for i = 0 to values.Length - 1 do printf $"{values.GetValue i} " // The example displays the following output: // Unhandled Exception: // System.IndexOutOfRangeException: Index was outside the bounds of the array. // at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices) // at System.Array.SetValue(Object value, Int32 index) // at <StartupCode$fs>.main@()Module Example Public Sub Main() Dim values = Array.CreateInstance(GetType(Integer), { 10 }, { 1 }) Dim value As Integer = 2 ' Assign values. For ctr As Integer = 0 To values.Length - 1 values(ctr) = value value *= 2 Next ' Display values. For ctr As Integer = 0 To values.Length - 1 Console.Write("{0} ", values(ctr)) Next End Sub End Module ' The example displays the following output: ' Unhandled Exception: ' System.IndexOutOfRangeException: Index was outside the bounds of the array. ' at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices) ' at System.Array.SetValue(Object value, Int32 index) ' at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateIndexSetComplex(Obje ' ct Instance, Object[] Arguments, String[] ArgumentNames, Boolean OptimisticSet, Boolean RV ' alueBase) ' at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateIndexSet(Object Instance, ' Object[] Arguments, String[] ArgumentNames) ' at Example.Main()Untuk memperbaiki kesalahan, seperti contoh berikut, Anda dapat memanggil GetLowerBound metode alih-alih membuat asumsi tentang indeks awal array.
using System; public class Example { public static void Main() { Array values = Array.CreateInstance(typeof(int), new int[] { 10 }, new int[] { 1 }); int value = 2; // Assign values. for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++) { values.SetValue(value, ctr); value *= 2; } // Display values. for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++) Console.Write("{0} ", values.GetValue(ctr)); } } // The example displays the following output: // 2 4 8 16 32 64 128 256 512 1024open System let values = Array.CreateInstance(typeof<int>, [| 10 |], [| 1 |]) let mutable value = 2 // Assign values. for i = values.GetLowerBound 0 to values.GetUpperBound 0 do values.SetValue(value, i) value <- value * 2 // Display values. for i = values.GetLowerBound 0 to values.GetUpperBound 0 do printf $"{values.GetValue i} " // The example displays the following output: // 2 4 8 16 32 64 128 256 512 1024Module Example Public Sub Main() Dim values = Array.CreateInstance(GetType(Integer), { 10 }, { 1 }) Dim value As Integer = 2 ' Assign values. For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0) values(ctr) = value value *= 2 Next ' Display values. For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0) Console.Write("{0} ", values(ctr)) Next End Sub End Module ' The example displays the following output: ' 2 4 8 16 32 64 128 256 512 1024Perhatikan bahwa ketika Anda memanggil GetLowerBound metode untuk mendapatkan indeks awal array, Anda juga harus memanggil Array.GetUpperBound(Int32) metode untuk mendapatkan indeks akhir.
Membingungkan indeks dan nilai pada indeks tersebut dalam array atau koleksi numerik. Masalah ini biasanya terjadi saat menggunakan
foreachpernyataan (dalam C#),for...inpernyataan (di F#), atauFor Eachpernyataan (di Visual Basic). Contoh berikut mengilustrasikan masalahnya.using System; public class Example { public static void Main() { // Generate array of random values. int[] values = PopulateArray(5, 10); // Display each element in the array. foreach (var value in values) Console.Write("{0} ", values[value]); } private static int[] PopulateArray(int items, int maxValue) { int[] values = new int[items]; Random rnd = new Random(); for (int ctr = 0; ctr < items; ctr++) values[ctr] = rnd.Next(0, maxValue + 1); return values; } } // The example displays output like the following: // 6 4 4 // Unhandled Exception: System.IndexOutOfRangeException: // Index was outside the bounds of the array. // at Example.Main()open System let populateArray items maxValue = let rnd = Random() [| for i = 0 to items - 1 do rnd.Next(0, maxValue + 1) |] // Generate array of random values. let values = populateArray 5 10 // Display each element in the array. for value in values do printf $"{values[value]} " // The example displays output like the following: // 6 4 4 // Unhandled Exception: System.IndexOutOfRangeException: // Index was outside the bounds of the array. // at <StartupCode$fs>.main@()Module Example Public Sub Main() ' Generate array of random values. Dim values() As Integer = PopulateArray(5, 10) ' Display each element in the array. For Each value In values Console.Write("{0} ", values(value)) Next End Sub Private Function PopulateArray(items As Integer, maxValue As Integer) As Integer() Dim values(items - 1) As Integer Dim rnd As New Random() For ctr As Integer = 0 To items - 1 values(ctr) = rnd.Next(0, maxValue + 1) Next Return values End Function End Module ' The example displays output like the following: ' 6 4 4 ' Unhandled Exception: System.IndexOutOfRangeException: ' Index was outside the bounds of the array. ' at Example.Main()Konstruksi iterasi mengembalikan setiap nilai dalam array atau koleksi, bukan indeksnya. Untuk menghilangkan pengecualian, gunakan kode ini.
using System; public class Example { public static void Main() { // Generate array of random values. int[] values = PopulateArray(5, 10); // Display each element in the array. foreach (var value in values) Console.Write("{0} ", value); } private static int[] PopulateArray(int items, int maxValue) { int[] values = new int[items]; Random rnd = new Random(); for (int ctr = 0; ctr < items; ctr++) values[ctr] = rnd.Next(0, maxValue + 1); return values; } } // The example displays output like the following: // 10 6 7 5 8open System let populateArray items maxValue = let rnd = Random() [| for i = 0 to items - 1 do rnd.Next(0, maxValue + 1) |] // Generate array of random values. let values = populateArray 5 10 // Display each element in the array. for value in values do printf $"{value} " // The example displays output like the following: // 10 6 7 5 8Module Example Public Sub Main() ' Generate array of random values. Dim values() As Integer = PopulateArray(5, 10) ' Display each element in the array. For Each value In values Console.Write("{0} ", value) Next End Sub Private Function PopulateArray(items As Integer, maxValue As Integer) As Integer() Dim values(items - 1) As Integer Dim rnd As New Random() For ctr As Integer = 0 To items - 1 values(ctr) = rnd.Next(0, maxValue + 1) Next Return values End Function End Module ' The example displays output like the following: ' 10 6 7 5 8Memberikan nama kolom yang tidak valid ke DataView.Sort properti .
Melanggar keamanan utas. Operasi seperti membaca dari objek yang sama StreamReader , menulis ke objek yang sama StreamWriter dari beberapa utas, atau menghitung objek dalam Hashtable dari utas yang berbeda dapat melemparkan IndexOutOfRangeException jika objek tidak diakses dengan cara yang aman utas. Pengecualian ini biasanya terputus-putus karena bergantung pada kondisi balapan.
Menggunakan nilai indeks yang dikodekan secara permanen untuk memanipulasi array kemungkinan akan melemparkan pengecualian jika nilai indeks salah atau tidak valid, atau jika ukuran array yang dimanipulasi tidak terduga. Untuk mencegah operasi melemparkan IndexOutOfRangeException pengecualian, Anda bisa melakukan hal berikut:
Iterasi elemen array menggunakan pernyataan foreach (dalam C#), untuk... dalam pernyataan (dalam F#), atau Untuk Setiap... Konstruksi berikutnya (dalam Visual Basic) alih-alih iterasi elemen menurut indeks.
Iterasi elemen dengan indeks yang dimulai dengan indeks yang dikembalikan oleh Array.GetLowerBound metode dan diakhir dengan indeks yang dikembalikan oleh Array.GetUpperBound metode .
Jika Anda menetapkan elemen dalam satu array ke array lainnya, pastikan bahwa array target memiliki setidaknya sebanyak elemen sebagai array sumber dengan membandingkan propertinya Array.Length .
Untuk daftar nilai properti awal untuk instance IndexOutOfRangeException, silakan lihat konstruktor IndexOutOfRangeException.
Instruksi bahasa perantara (IL) berikut melemparkan IndexOutOfRangeException:
Ldelem.<Jenis>
ldelema
stelem.<Jenis>
IndexOutOfRangeException menggunakan COR_E_INDEXOUTOFRANGE HRESULT, yang memiliki nilai 0x80131508.
Konstruktor
| Nama | Deskripsi |
|---|---|
| IndexOutOfRangeException() |
Menginisialisasi instans baru dari kelas IndexOutOfRangeException. |
| IndexOutOfRangeException(String, Exception) |
Menginisialisasi instans IndexOutOfRangeException baru kelas dengan pesan kesalahan tertentu dan referensi ke pengecualian dalam yang merupakan penyebab pengecualian ini. |
| IndexOutOfRangeException(String) |
Menginisialisasi instans IndexOutOfRangeException baru kelas dengan pesan kesalahan tertentu. |
Properti
| Nama | Deskripsi |
|---|---|
| Data |
Mendapatkan kumpulan pasangan kunci/nilai yang memberikan informasi tambahan yang ditentukan pengguna tentang pengecualian. (Diperoleh dari Exception) |
| HelpLink |
Mendapatkan atau mengatur tautan ke file bantuan yang terkait dengan pengecualian ini. (Diperoleh dari Exception) |
| HResult |
Mendapatkan atau mengatur HRESULT, nilai numerik berkode yang ditetapkan ke pengecualian tertentu. (Diperoleh dari Exception) |
| InnerException |
Mendapatkan instans Exception yang menyebabkan pengecualian saat ini. (Diperoleh dari Exception) |
| Message |
Mendapatkan pesan yang menjelaskan pengecualian saat ini. (Diperoleh dari Exception) |
| Source |
Mendapatkan atau mengatur nama aplikasi atau objek yang menyebabkan kesalahan. (Diperoleh dari Exception) |
| StackTrace |
Mendapatkan representasi string dari bingkai langsung pada tumpukan panggilan. (Diperoleh dari Exception) |
| TargetSite |
Mendapatkan metode yang melemparkan pengecualian saat ini. (Diperoleh dari Exception) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetBaseException() |
Ketika ditimpa di kelas turunan, mengembalikan Exception yang merupakan akar penyebab dari satu atau beberapa pengecualian berikutnya. (Diperoleh dari Exception) |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Kedaluwarsa.
Saat ditimpa di kelas turunan, mengatur SerializationInfo dengan informasi tentang pengecualian. (Diperoleh dari Exception) |
| GetType() |
Mendapatkan jenis runtime instans saat ini. (Diperoleh dari Exception) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Membuat dan mengembalikan representasi string dari pengecualian saat ini. (Diperoleh dari Exception) |
Acara
| Nama | Deskripsi |
|---|---|
| SerializeObjectState |
Kedaluwarsa.
Terjadi ketika pengecualian diserialisasikan untuk membuat objek status pengecualian yang berisi data berseri tentang pengecualian. (Diperoleh dari Exception) |