Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
189 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...