O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

int[] nums = new[] { 1, 2, 3 };for (int i = 0; i <= nums.Length; i++)    nums[i]++;

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 should be able ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required