O'Reilly logo

Applied Microsoft® .NET Framework Programming by Jeffrey Richter

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

Fast Array Access

Each time an element of an array is accessed, the CLR ensures that the index is within the array’s bounds. This prevents you from accessing memory that is outside of the array, which would potentially corrupt other objects. If an invalid index is used to access an array element, the CLR throws a System.IndexOutOfRangeException exception.

As you might expect, the CLR’s index checking comes at a performance cost. If you have confidence in your code and if you don’t mind resorting to nonverifiable (unsafe) code, you can access an array without having the CLR perform its index checking. The following C# code demonstrates this approach:

using System; class App { unsafe static void Main() { // Construct an array consisting of five Int32 ...

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