Stats

Contributors: 2
2016-11-01
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook

Member access using ->

Download c# eBook

Example

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 v;
v.X = 5;
v.Y = 10;

Vector2* ptr = &v;
int x = ptr->X;
int y = ptr->Y;
string s = ptr->ToString();

Console.WriteLine(x); // prints 5
Console.WriteLine(y); // prints 10
Console.WriteLine(s); // prints Vector2