Tutorial by Examples

This example demonstrates how pointers can be used for C-like access to C# arrays. unsafe { var buffer = new int[1024]; fixed (int* p = &buffer[0]) { for (var i = 0; i < buffer.Length; i++) { *(p + i) = i; } } } The unsafe keyw...
Addition and subtraction in pointers works differently from integers. When a pointer is incremented or decremented, the address it points to is increased or decreased by the size of the referent type. For example, the type int (alias for System.Int32) has a size of 4. If an int can be stored in add...
In C and C++, the asterisk in the declaration of a pointer variable is part of the expression being declared. In C#, the asterisk in the declaration is part of the type. In C, C++ and C#, the following snippet declares an int pointer: int* a; In C and C++, the following snippet declares an int ...
C# inherits from C and C++ the usage of void* as a type-agnostic and size-agnostic pointer. void* ptr; Any pointer type can be assigned to void* using an implicit conversion: int* p1 = (int*)IntPtr.Zero; void* ptr = p1; The reverse requires an explicit conversion: int* p1 = (int*)IntPtr.Ze...
C# inherits from C and C++ the usage of the symbol -> as a means of accessing the members of an instance through a typed pointer. Consider the following struct: struct Vector2 { public int X; public int Y; } This is an example of the usage of -> to access its members: Vector2...
The criteria that a type must satisfy in order to support pointers (see Remarks) cannot be expressed in terms of generic constraints. Therefore, any attempt to declare a pointer to a type provided through a generic type parameter will fail. void P<T>(T obj) where T : struct { T* p...

Page 1 of 1