textBox1.Text.Insert(…)方法不起作用

我正面临这种不正常的情况。 以下代码无法正常运行:

string temp = "heythere"; Console.WriteLine(temp); temp.Insert(3, "hello"); Console.WriteLine(temp); 

是不是应该输出像“heyhellothere”? 但它确实“heyrehere”(没有变化)。

字符串是不可变的,它们不会就地更改。 尝试:

 string temp = "heythere"; Console.WriteLine(temp); temp = temp.Insert(3, "hello"); Console.WriteLine(temp); 

或者,你可以试试这个

 string temp = "heythere"; Console.WriteLine(temp.Insert(3, "hello"));