小编use*_*371的帖子

更改方法内的引用类型(字符串)

我将字符串变量传递给方法.我知道字符串是引用类型,但我在方法中分配的值丢失了.

public static void TestMethod(string myString)
{
    myString = "world";
}

static void Main(string[] args)
{
    string s = "hello";
    Console.WriteLine(s); // output is "hello"
    TestMethod(s);
    Console.WriteLine(s); // output is also "hello" not "world" !?
}
Run Code Online (Sandbox Code Playgroud)

无论如何,例如,数组不会发生这种情况.有人可以解释为什么可能是原因吗?

c# oop

4
推荐指数
1
解决办法
2085
查看次数

标签 统计

c# ×1

oop ×1