Game Development Reference
In-Depth Information
marshaling has occurred. System.Runtime.InteropServices.Marshal.SizeOf() returns
the size of a data type when converted to an unmanaged representation. Using
Marshal.SizeOf(char) will return one byte since at this point the char has been con-
verted to a one-byte ANSI character.
You can also get the size of a struct that contains value types. The following code
shows this.
public struct SimpleStruct
public char firstChar;
public char secondChar;
public int myInteger;
Evaluating sizeof(SimpleStruct) will return 10 bytes with padding,
Marshal.SizeOf(SimpleStruct) will return 6 bytes.
Example: Array Iteration and Value Assignment
The following example shows how to loop through an array and assign values to
each array element. This example also profiles the elapsed time between using
checked and unchecked arithmetic.
private static void ArrayValueAssignment()
byte[] data = new byte[100000000];
int unsafeTime = 0;
int uncheckedTime = 0;
unsafeTime = Environment.TickCount;
fixed (byte* dataPtr = data)
byte* dataByte = dataPtr;
for (int index = 0; index < data.Length; index++)
*dataByte++ = (byte)index;
// Can also do: *(dataByte + index) = (byte)index;
Search Nedrilad ::

Custom Search