ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection
Code accessed a List/collection position that doesn’t exist (IndexOutOfRangeException for arrays).
Seen on:
.NET
Meaning
Off-by-one loops, empty lists, or indices from another collection. Arrays throw IndexOutOfRangeException (“Index was outside the bounds of the array”).
Common causes
- Accessing [0] on an empty list
- Loop using <= Count
- Index from a different collection
- Concurrent removal
⚡ Quick fix
- Check Count before indexing; use FirstOrDefault
- Fix loop bounds (< Count)
- Use ElementAtOrDefault
Detailed fix by platform
C#
- csharp
var first = list.FirstOrDefault(); for (var i = 0; i < list.Count; i++) { /* ... */ }
How to diagnose
- Index — Value vs Count
- Source — Which collection?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in .NET
- NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object
- FileNotFoundException assembly Could not load file or assembly 'X, Version=…' or one of its dependencies
- InvalidOperationException InvalidOperationException: Unable to resolve service for type 'X' while attempting to activate 'Y'
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026