
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1072
|
Chapter 19: Unsafe Code
Console.WriteLine(*(arrayPtr + 4)); //Display the last value '5'
}
}
Creating a pointer to an array of enumeration values is very similar:
unsafe
{
Colors[] intArray = new Colors[2] {Colors.Red, Colors.Blue};
fixed(Colors* arrayPtr = intArray)
{
// Use arrayPtr here.
}
}
where Colors is declared as follows:
public enum Colors{Red, Green, Blue}
Creating a pointer to a multidimensional array is performed by declaring and initial-
izing a multidimensional array:
int[,] intMultiArray = new int[2,5] {{1,2,3,4,5},{6,7,8,9,10}};
You then set a pointer to the address of the first element in this array:
fixed(int* arrayPtr = intMultiArray)
For example, the following code creates and initializes a pointer to a multidimen-
sional array, then displays the last item in that array:
unsafe
{
int[,] intMultiArray = new int[2,5] {{1,2,3,4,5},{6,7,8,9,10}};
fixed(int* arrayPtr = intMultiArray)
{
Console.WriteLine(*(arrayPtr + 9)); //Display the last value '10'
}
}
See Also
See the “Multidimensional Arrays” and “Jagged Arrays” topics and the “Unsafe at
the Limit” article in the MSDN documentation.
19.4 Manipulating a Pointer to a Fixed Array
Problem
One limitation of a pointer to a fixed array is that you cannot reassign this pointer to
any other element of that array using pointer ...