Hello Friends,
Today our topic is on IndexOutOfRange Exception in C#.
The IndexOutOfRange Exception will arise only when the index is not present but we are trying to access particular index.
Example:-
------------
int[] arr={1,2,3,4,5};
for(int i=0;i<=arr.Length;i++)
{
Console.WriteLine(arr[i]);
}
Console.ReadLine();
Whenever this application runs then it will throw IndexOutOfRange Exception because as per the code we are trying to access the 5th value which is not present in the array.
So whenever we are accessing any index value then make sure that should exists in array otherwise it will throw IndexOutOfRange Exception.
To handle or avoid this issue, we need to remove = from for loop so that it will loop upto only 4th value.
Also to handle this error i can use foreach loop.
Wish You Success,
#VCreationsTech.
Негізгі бет IndexOutOfRange Exception in C#
Пікірлер: 7