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"?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Munesh SharmaPosted Oct 13, 2014, 12:29 AM
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:
VulpesPosted Oct 12, 2014, 9:27 AM
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.