Source array was not long enough. Check srcIndex and length, and the array's lower bounds
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
This .NET error means a copy operation tried to read past the available part of the source array. The usual cause is a mismatch between srcIndex, length, and the actual source array size. In practice, it often appears in calls such as Array.Copy, Buffer.BlockCopy, or framework code that internally performs array copying.
What the Error Really Means
A typical copy operation needs all of these to be valid:
- '
srcIndexmust be within the source array' - '
lengthmust be non-negative' - '
srcIndex + lengthmust not exceed the source array bounds' - the destination side must also have enough space
If any of those relationships fail, the runtime throws an error instead of reading invalid memory.
A direct example:
This fails because starting at source index 2, only one element remains, but the code asks to copy two.
Fix the Index Math First
The fastest way to debug this class of problem is to print or inspect the values that control the copy.
Then check the relationship explicitly.
A guarded copy pattern:
This makes the bug easier to catch where the invalid range is created rather than letting it surface later in a less obvious place.
Watch for Off-by-One Errors
Many of these failures are simple off-by-one mistakes.
For example, if the last valid source index is source.Length - 1, then copying 2 items from that index is impossible because there is only one item left.
A common faulty pattern is:
- loop until
i <= array.Length - then use
ias a valid index
That crosses the valid boundary by one element.
Array.Copy Versus Buffer.BlockCopy
The error message often appears around Array.Copy, but similar logic applies to Buffer.BlockCopy. The important difference is that Buffer.BlockCopy uses byte counts, not element counts.
That copies two bytes, not two higher-level objects. When developers forget that distinction and pass an element count where a byte count is required, range errors follow quickly.
The Error May Come from Framework Code
Sometimes your own code never calls Array.Copy directly, but a serializer, collection helper, or parser does. In that case, the same debugging rule applies: find the array, index, and requested length that the framework code received.
That often means the real bug is earlier, such as:
- wrong slice length computed from a packet header
- malformed input data
- incorrect assumption about fixed-size records
- stale cached array size after a resize
The thrown exception is just where the bad range finally becomes visible.
Common Pitfalls
A common mistake is checking only the destination length and forgetting that the source side can fail first.
Another mistake is confusing "last valid index" with "number of remaining items". Those are not the same quantity.
People also often misuse Buffer.BlockCopy by passing element counts instead of byte counts.
Finally, if a framework throws the exception, do not stop at the stack frame that copies the array. Trace back to where the invalid index and length were calculated.
Summary
- This error means a copy operation tried to read beyond the available portion of the source array
- Validate
srcIndex,length, andsrcIndex + lengthbefore copying - Check the destination range too, but do not forget the source side
- Off-by-one mistakes are one of the most common causes
- '
Buffer.BlockCopyuses byte counts, which is easy to misuse' - When the error comes from framework code, the real bug is often earlier in your own range calculation logic
Related reading
- Space-efficient algorithm for finding the largest balanced subarray?
- Space-efficient probabilistic data structures for number retrieval
- space complexity of merge sort using array
- Spanning tree which minimizes the number of vertices connected to multiple edges?
- SourceKitService Consumes CPU and Grinds Xcode to a Halt
- SourceKitService Terminated
- Sparse matrices / arrays in Java
- SparseArray vs HashMap

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.