由于字符串是 dotnet 中的 ref 类型,当我们更新变量 x 时,y 中也应该有更新?(因为它是 x 的参考值)。下面给出的示例程序,当 x 更新时 y 的值如何不改变?
public void assignment()
{
string x= "hello";
string y=x; // referencing x as string is ref type in .net
x = x.Replace('h','j');
Console.WriteLine(x); // gives "jello" output
Console.WriteLine(y); // gives "hello" output
}
Run Code Online (Sandbox Code Playgroud)