tags:

views:

279

answers:

8
+2  Q: 

Using "->" in c#?

Hi, I have remembered that I have seen "->" used in C#. Apparently I cannot search it through Google (and I do not know what is the name). Therefore I would be very happy if you could explain it to me. Thanks.

+2  A: 

Do you mean Lambda Expressions?

Noon Silk
No, that's =>, not ->.
wj32
wj32: Yeah, hence the reason my post started with "Do you mean ...".
Noon Silk
+19  A: 

-> Accesses a member of a struct through a pointer.

See -> Operator

and

Pointer Types

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 );
   }
}

Outputs

123 456
rahul
+2  A: 

From MSDN

The -> operator combines pointer dereferencing and member access

Richard Szalay
+1  A: 

It's used the same way it's used in C - to access a field in a struct using a pointer. To use pointers in C#, you need to have /unsafe enabled. For example:

unsafe
{
    MyStruct* ptr;

    Console.WriteLine(ptr->SomeField.ToString());
}
wj32
A: 

It's an operator that is not commonly used, only in unsafe code

http://msdn.microsoft.com/en-us/library/s8bz4d5h(VS.80).aspx

Peter
unsafe code is a language feature of C#. What does make its usage un-normal, then?
Joey
Johannes: What? Are you normally writing lots of unsafe functions, then? If so, good for you, but it's not *normal*, in general c# code.
Noon Silk
Unsafe code makes the code unverifiable and works against .NET concepts as garbage collection etc..It is mostly used for calling native APIs.
Arve
s/normally/commonly/
el.pescado
silky: No, I don't. But I'd consider `unsafe` to be no less normal than lambdas or LINQ. el.pescado suggests a nice alternative :-)
Joey
Johannes: Maybe it's a language thing; in English it is quite acceptable to use "normally" in the fashion above.
Noon Silk
@all wow, .... I indeed meant commonly, so sorry , i'll correct it
Peter
+4  A: 

The -> operator is used for dereferencing pointers, and is used only in unsafe C# code.

Pointers are more or less like object references, except that they point to a memory address. It's a little closer to the internal workings of the system, but it puts garbage collection and type safety out of play. References are more abstract and more restrictive, but allow the runtime to guarantee type safety and do memory management.

See also this MSDN article about unsafe code: http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx

Thorarin
A: 

Exactly the same as in C++. But to have pointers in C# requires "unsafe" code and you don't want to do that too much...

    static unsafe void Main(string[] args)
    {
        int x = 5;
        int* p = &x;

        Console.WriteLine(p->ToString());
    }
Arve
+1  A: 

Yes ofcourse it is used ..
the operator -> is used to access the members of a struct (its a pointer).

MSDN Site would be better for seeking information on VB, C# and etc .. here is the link

infant programmer