-> Operator
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

-> Operator

The -> operator combines pointer dereferencing and member access.

expr1 -> expr2

Where:

expr1
A pointer expression.
expr2
An expression.

Remarks

An expression of the form

x->y

(where x is a pointer of type T* and y is a member of T) is equivalent to

(*x).y

The -> operator can be used only in unmanaged code.

The -> operator cannot be overloaded.

Example

// cs_operator_dereferencing.cs
// compile with: /unsafe
using System;
struct Point
{
   public int x, y; 
}

class Test 
{
   public unsafe static void Main() 
   {
      Point pt = new Point();
      Point* pp = &pt;
      pp->x = 123;
      pp->y = 456;
      Console.WriteLine ( "{0} {1}", pt.x, pt.y );
   }
}

Output

123 456

See Also

C# Operators | A.2 Pointer types

Show:
© 2016 Microsoft