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

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

问题描述

我试图学习C#,我熟悉C ++结构指向符号 - >我很好奇,如果这跨进了C#。

I am trying to learn C# and I am familiar with the C++ struct pointing notation -> and I was curious if that crossed over into C#.

示例: / p>

example:

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

看起来像这样链接):

struct MyStruct 
{ 
    public long X; 
    public double D; 
}

unsafe static void foo() {
   MyStruct myStruct = new MyStruct(); 
   pMyStruct = & myStruct;

   // access:

   (*pMyStruct).X = 18; 
   (*pMyStruct).D = 163.26;

   // or

   pMyStruct->X = 18; 
   pMyStruct->D = 163.26;
}

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

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