​​Solutions to Prevent IndexOutOfRangeException (part 2)



Solution 1: Get the total number of elements in a collection and then check the upper bound of a collection is one less than its number of elements.



The following example shows how to fix IndexOutOfRange exception: