Tag: ref

为什么在传递对象时使用'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; } } 输出是“Bar”,这意味着对象被作为参考传递。