IndexOutOfRangeException

Another exception in the category of silly bug revealers is IndexOutOfRangeException, which indicates an array is accessed outside its boundaries. “Off-by-one” errors are a typical source of this exception:

image

Bounds checks have been a historical source of bugs, which often went unnoticed in the world of native code, causing random program crashes because of memory corruption. Consider yourself lucky that the CLR catches this class of problems using runtime checks (to avoid its memory and type safety), but don’t write such bugs in the first place! Off-by-one errors can be subtle, but with a little bit of extra thought, you ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.