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

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

问题描述

我正在尝试学习 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# 是否使用 ->指针符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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