I am trying to learn C# and I am familiar with the C++ struct pointing notation ->
. I was curious if that crossed over into C#.
Example:
someStruct->someAttribute += 1;
See Question&Answers more detail:osI am trying to learn C# and I am familiar with the C++ struct pointing notation ->
. I was curious if that crossed over into C#.
Example:
someStruct->someAttribute += 1;
See Question&Answers more detail:osThere is pointer notation in C#, but only in special cases, using the unsafe
keyword.
Regular objects are dereferenced using .
, but if you want to write fast code, you can pin data (to avoid the garbage collector moving stuff around) and thus "safely" use pointer arithmetic, and then you might need ->
.
See Pointer types (C# Programming Guide) and a bit down in this example on the use of ->
in C#.
It looks something like this (from the last link):
struct MyStruct
{
public long X;
public double D;
}
unsafe static void foo()
{
var myStruct = new MyStruct();
var pMyStruct = & myStruct;
// access:
(*pMyStruct).X = 18;
(*pMyStruct).D = 163.26;
// or
pMyStruct->X = 18;
pMyStruct->D = 163.26;
}