How to access structure elements using Pointers in C#
Unlike C/C++, Structures in C# can have members that are methods, fields, indexers, operator methods, properties or events. The members can have access specifiers as public, private, and internal.
Pointers are variables that store the addresses of the same type of variable i.e. an int pointer can store an address of an integer, a char pointer can store an address of a char and similarly for all other data types, fundamental or user-defined.
You can access a structure member using pointers, of type structure, in the following ways;
1) Using the arrow operator: If the members of the structure are public then you can directly access them using the arrow operator ( -> ). If they are private then you can define methods for accessing the values and use pointers to access the methods. The arrow operator can be used to access structure variables as well as methods.
2) Using Dereferencing operator: You can also access structure elements using the dereferencing operator on the pointer, which is using an asterisk to dereference the pointer and then using the dot operator to specify the structure element.
Note: To compile unsafe code on Visual Studio (2012), Go to Project –> ProjectName Properties –> Build –> Check the “Allow unsafe code” box.