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.
->
Accesses a member of a struct through a pointer.
See -> Operator
and
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
From MSDN
The
->
operator combines pointer dereferencing and member access
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());
}
It's an operator that is not commonly used, only in unsafe code
http://msdn.microsoft.com/en-us/library/s8bz4d5h(VS.80).aspx
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
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());
}
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