Essential C# Unsafe Code in .NET  Suggest an edit

Unsafe Array Index

void Main()
{
    unsafe
    {
        int[] a = {1, 2, 3};
        fixed(int* b = a)
        {
            Console.WriteLine(b[4]);
        }
    }
}

Running this code creates an array of length 3, but then tries to get the 5th item (index 4). On my machine, this printed 1910457872, but the behavior is not defined.

Without the unsafe block, you cannot use pointers, and therefore cannot access values past the end of an array without causing an exception to be thrown.


Table Of Contents
22 Enum
25 GUID
107 Stream
108 Timers
147 Unsafe Code in .NET
  ↑ ↓ to navigate     ↵ to select     Esc to close