Does C# use the -> pointer notation?

前端 未结 4 904
南旧
南旧 2020-12-11 03:02

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:



        
4条回答
  •  感动是毒
    2020-12-11 03:54

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

提交回复
热议问题