Game Development Reference
Pointers in C# should not be used except for the three situations listed above. Only
use pointers when absolutely necessary.
Never use pointers as an attempt to write C code in C#.
Before continuing on, it is important to list the advantages and disadvantages of
using C# pointers.
The advantages are:
Enhanced performance and increased flexibility. You can use a pointer to
access and manipulate data in the most efficient way.
There have obviously been a large number of Windows and third-party
libraries that were developed prior to the .NET platform. Some functions
may require that pointers be passed as parameters. Though this can be
accomplished with DLLImport and System.IntPtr , it can often be cleaner to
do it with pointers if you are already using them. Pointers offer extensive
compatibility with legacy components.
Some situations require that you track memory addresses, in which case a
pointer is the only way to accomplish this.
The disadvantages are:
Using pointers in C# increases the complexity of the language syntax. While
C\C++ developers are accustomed to it, C# developers may struggle a bit
with the rarely used concepts.
Pointers are much harder to use, and even harder to use safely, than using
reference types. It is quite easy to overwrite other variables, cause stack
overflows, and access areas of memory that do not contain valid data, and
in some cases, you can even overwrite process data for the .NET runtime.
Doing so will result in a fatal application crash, defeating the purpose of
using managed code for robust fault tolerance in the first place.
Now that I have successfully scared you away from using pointers in C#, it is time
to continue on into the implementation and usage details.
Rudiments of Pointer Notation
The concept of pointers is well known and loved by C++ developers, but develop-
ers accustomed to other languages may find the idea and syntax difficult to grasp