Member access using -> C# with Example



Member access using -> C# with 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 

0 Comment's

Comment Form

Submit Comment