C# immutable int

前端 未结 3 500
野性不改
野性不改 2021-02-05 12:38

In Java strings are immutable. If we have a string and make changes to it, we get new string referenced by the same variable:

String str = \"abc\";
str += \"def\         


        
3条回答
  •  伪装坚强ぢ
    2021-02-05 13:02

    To follow up on Marc's (perfectly acceptable) answer: Integer values are immutable but integer variables may vary. That's why they're called "variables".

    Integer values are immutable: if you have the value that is the number 12, there's no way to make it odd, no way to paint it blue, and so on. If you try to make it odd by, say, adding one, then you end up with a different value, 13. Maybe you store that value in the variable that used to contain 12, but that doesn't change any property of 12. 12 stays exactly the same as it was before.

提交回复
热议问题