How to solve this problem"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"?
Problem "Index was out of range"
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

VulpesRank #55 · 29.2K reputationPosted Oct 12, 2014, 5:27 AM EDT
Perhaps the commonest cause is when trying to access the last element. Suppose 'a' is an int array and you try to access the last element with code such as this:
int last = a[a.Length];
This will throw an 'index out of range exception' because the last element has an index of a.Length - 1, not a.Length, remembering that arrays are always zero based in C#.
If that doesn't appear to be the cause of the error here, can you post the code so we can try and figure out what else might be causing it.
Munesh SharmaRank #174 · 13.9K reputationPosted Oct 12, 2014, 8:29 PM EDT
For example, if you have a list or an array that you are accessing in a loop, the loop counter must be less than the items count: