如果我将一个对象传递给一个方法,为什么我应该使用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”,这意味着对象作为引用传递。
将引用类型(例如List<T>)的变量(例如foo)视为持有形式为“object# 24601”的对象标识符。假设语句foo = new List<int> {1,5,7,9};使foo保存“Object #24601”(一个包含四个项的列表)。然后调用foo。Length将向object# 24601请求其长度,它将响应4,因此输入foo。长度等于4。
如果foo被传递给一个方法而没有使用ref,该方法可能会对Object #24601进行更改。作为这些变化的结果,foo。Length可能不再等于4。然而,方法本身将无法更改foo,它将继续保存“object# 24601”。
将foo作为ref参数传递将允许被调用的方法不仅对Object #24601进行更改,而且对foo本身进行更改。该方法可以创建一个新的对象#8675309,并在foo中存储对该对象的引用。如果这样做,foo将不再保存“object# 24601”,而是保存“object# 8675309”。
In practice, reference-type variables don't hold strings of the form "Object #8675309"; they don't even hold anything that can be meaningfully converted into a number. Even though each reference-type variable will hold some bit pattern, there is no fixed relationship between the bit patterns stored in such variables and the objects they identify. There is no way code could extract information from an object or a reference to it, and later determine whether another reference identified the same object, unless the code either held or knew of a reference that identified the original object.