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

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

c++ - Does C# use the -> pointer notation?

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

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...