C#是否使用-& gt;指针符号? [英] Does C# use the -> pointer notation?

查看:53
本文介绍了C#是否使用-& gt;指针符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习C#,并且熟悉C ++结构指向符号-> .我很好奇这是否会渗透到C#中.

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#.

示例:

someStruct->someAttribute += 1;

推荐答案

C#中有指针符号,但仅在特殊情况下,使用 unsafe 关键字.

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 ->.

请参见指针类型(C#编程指南)此示例关于在C#中使用-> .

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

这篇关于C#是否使用-& gt;指针符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆