Writing Past the End of an Array

When you reference an element in an array, the compiler (unfortunately) does not care whether the element actually exists. It computes how far past the first element it should go and then generates instructions that get or change that location's content. If the element is past the bounds of the array, this can be virtually anything and the results will be unpredictable. If you're lucky, your program will crash immediately or throw a bounds violation exception. If you're unlucky, you may get strange results much further on in your program run. You need to make sure that the condition in your for statement will cause the loop to stop before it goes past the bounds of the array. The condition expression Index < NumberOfElements is always certain to accomplish this, because the highest valid index is always one less than the count of elements in the array.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.149.116