相关疑难解决方法(0)

C#immutable int

在Java中,字符串是不可变的.如果我们有一个字符串并对其进行更改,我们将获得由同一个变量引用的新字符串:

String str = "abc";
str += "def"; // now str refers to another piece in the heap containing "abcdef"
              // while "abc" is still somewhere in the heap until taken by GC
Run Code Online (Sandbox Code Playgroud)

据说 int和double在C#中是不可变的.这是否意味着当我们有int并稍后更改它时,我们会得到同一个变量"指向"的新int?同样的事情,但堆栈.

int i = 1;
i += 1; // same thing: in the stack there is value 2 to which variable
        // i is attached, and somewhere in the stack there is value 1
Run Code Online (Sandbox Code Playgroud)

那是对的吗?如果没有,int以什么方式不可变?

c# int immutability

15
推荐指数
2
解决办法
1591
查看次数

标签 统计

c# ×1

immutability ×1

int ×1