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

查看:32
本文介绍了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' 是一种值类型,因此正如您所发现的那样,复制赋值时的值.

'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天全站免登陆