Essential C# Unsafe Code in .NET  Suggest an edit

Unsafe Array Index

void Main()
        int[] a = {1, 2, 3};
        fixed(int* b = a)

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
107 Stream
108 Timers
147 Unsafe Code in .NET
  ↑ ↓ to navigate     ↵ to select     Esc to close