Unsafe Code in .NET

suggest change

Remarks

For instance, when you use a for loop over an array like so:

for (int i = 0; i < array.Length; i++)
{
    array[i] = 0;
}

The runtime ensures that you do not exceed the bounds of the array, throwing an IndexOutOfRangeException if the index exceeds the bounds.

However, if you use unsafe code, you may exceed the array’s bounds like so:

unsafe
{
    fixed (int* ptr = array)
    {
        for (int i = 0; i <= array.Length; i++)
        {
            *(ptr+i) = 0;
        }
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents