C#中引用赋值运算符? [英] C# reference assignment operator?

查看:345
本文介绍了C#中引用赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

        int x = 1;
        int y = x;
        y = 3;
        Debug.WriteLine(x.ToString());



这是任何引用运营商,而不是=上线:3,
,使在x等于3,如果我给你Y = 3。

Is it any reference operator instead of "=" on line:3, to make the x equal to 3 , if i assign y =3 .

推荐答案

'廉政'是一个价值型等值拷贝执行任务,因为你已经发现。

'int' is a value type and so copies the value on assignment, as you have discovered.

您可以通过使用不安全块但随后的三分球只能是块这就限制了它的使用可用的内部使用指针在传统的C / C ++的感觉。相反,如果你想要一个不安全块的外部访问值,那么你需要转换为使用引用来代替。

You could use pointers in the traditional C/C++ sense by using an 'unsafe' block but then the pointers are only usable inside that block which limits its use. If instead you want to access the value outside of an 'unsafe' block then you need to convert to using a reference instead.

这样的事情...

var x = new Tuple<int>(1);
var y = x;
y.Item1 = 3;

这篇关于C#中引用赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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