相关疑难解决方法(0)

为什么在传递对象时使用'ref'关键字?

如果我将对象传递给方法,为什么要使用ref关键字?这不是默认行为吗?

例如:

class Program
{
    static void Main(string[] args)
    {
        TestRef t = new TestRef();
        t.Something = "Foo";

        DoSomething(t);
        Console.WriteLine(t.Something);
    }

    static public void DoSomething(TestRef t)
    {
        t.Something = "Bar";
    }
}


public class TestRef
{
    public string Something { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

输出为"Bar",表示该对象作为参考传递.

.net c# ref pass-by-reference

269
推荐指数
7
解决办法
15万
查看次数

标签 统计

.net ×1

c# ×1

pass-by-reference ×1

ref ×1