Thursday, October 4, 2018

Published 2:41 AM by with 0 comment

What is IndexOutOfRangeException?


What is System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection OR IndexOutOfRangeException?

Solution:

1.What Is meant of ArgumentOutOfRangeException OR IndexOutOfRangeException?



In C#,When you try to access a collection of item in an array through using an invalid index  it say IndexOutOfRangeException. 

When an index is invalid?

An index of an array is invalid when its lower that the collection's lower bound or greater than or equal to the number of items the collection contains.

when it throw an IndexOutOfRangeException?

Given an array is declares as:

            
var idList = new int[4] { 8, 10, 11, 12 };

The amount in the square bracket is index/es.it range is 4 means you access this array from 0 to 3.and  8,10,11,12 are the items it contain, when you try to access this value outside this range it will through exception IndexOutOfRangeException. So remember this when you try to access any array.Range/Length of arrays:
In C#, arrays range is start from 0 index and its end on range-1(where the range is total number of items an array contains ).
so this code will not work :

Itemlist = idList[10]
because indexes range is 4 and we call 10.





      edit

0 comments:

Post a Comment