C# reference assignment operator?

后端 未结 6 570
滥情空心
滥情空心 2020-12-11 08:28

for example:

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

Is it any reference operator inste

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 08:50

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

    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.

    Something like this...

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

提交回复
热议问题