gpt4 book ai didi

c# - 关于C#中引用字符串的问题

转载 作者:行者123 更新时间:2023-12-03 19:20:28 25 4
gpt4 key购买 nike

快速提问。

采用以下代码示例:

ArrayList arrayList = new ArrayList();
string testString = "";
testString = "test";
arrayList.Add(testString);

在该代码示例中,如果我要执行以下操作:

arrayList[0] = "anotherTest";

testString 会更改为 anotherText 还是保持不变?是放入的对象还是它的副本、另一个字符串?

谢谢,克里斯蒂安

最佳答案

之前:

          Before

之后:

          After

  • 仅将 a​​rrayList[0] 的值更改为对“anotherTest”的引用。 testString 变量的值保持不变。

  • 字符串是引用类型。这意味着 testString 变量和 arrayList[0] 各自(独立)保存对对象“test”和“anotherTest”的引用,而不是值本身。

  • 将 testString 或 arrayList[0] 从一个字符串更改为另一字符串不会修改原始字符串。字符串是不可变的。仅更改对字符串对象的引用。

关于c# - 关于C#中引用字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6555529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com