A dictionary is a reference type, so it is not possible to pass by value, although references to a dictionary are values. Let me try to clear this up:
void Method1(Dictionary<string, string> dict) {
dict["a"] = "b";
dict = new Dictionary<string, string>();
}
void Method2(ref Dictionary<string, string> dict) {
dict["e"] = "f";
dict = new Dictionary<string, string>();
}
public void Main() {
var myDict = new Dictionary<string, string>();
myDict["c"] = "d";
Method1(myDict);
Console.Write(myDict["a"]); // b
Console.Write(myDict["c"]); // d
Method2(ref myDict); // replaced with new blank dictionary
Console.Write(myDict["a"]); // key runtime error
Console.Write(myDict["e"]); // key runtime error
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…