小编R74*_*74M的帖子

C#中的字符串引用

由于字符串是 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)

.net c# string

3
推荐指数
1
解决办法
107
查看次数

标签 统计

.net ×1

c# ×1

string ×1