Is really argument passing by value?
This question already has an answer here:
After reading the page, what he is saying is this.
Now remember that the value of a reference type variable is the reference, not the object itself. You can change the contents of the object that a parameter refers to without the parameter itself being passed by reference.
public static void Main()
{
var s = new StringBuilder();
Console.WriteLine(s.ToString());
SayHello(s);
Console.WriteLine(s.ToString());
Console.ReadLine();
}
private static void SayHello(StringBuilder s)
{
s.AppendLine("Hello");
s = null;
}
//output will be Hello
When this method is called, the parameter value (a reference to a StringBuilder) is passed by value. If I were to change the value of the builder variable within the method—for example, with the statement builder = null;—that change wouldn't be seen by the caller, contrary to the myth.
So pretty much he explain that you can't destroy the object StringBuilder
in SayHello, you can just change the content of this object. I didn't know that too.
EDIT: About your comment
So pretty much when you pass s
in SayHello, you are creating new reference and both reference in Main
method and in SayHello
refer to the same object. But they exist like 2 different references.
If you write in
private static void SayHello(StringBuilder s)
{
s.AppendLine("Hello");
s = new StringBuilder();
}
The reference of the s in SayHello
will point to another object, but this will not change anything in Main method because the reference is pointing to previous object in which you wrote Hello. So again the output will be Hello.
If you want to pass the exact reference you should use ref
So if you write
private static void SayHello(ref StringBuilder s)
{
s.AppendLine("Hello");
s = null;
//you will receive an exception in Main method, because now the value in the reference is null.
}
Consider the following analogy:
new StringBuilder()
) and you get a key ( s
) that grants you access. s.AppendLine
) and you give him a copy of your key so he can access your apartment and clean it up. That's basically what happens when you pass a reference type by value.
Now consider the following:
new StringBuilder()
) and you get a key ( s
) that grants you access. s.AppendLine
) and you give him your key so he can clean up your apartment. Thats what happens when you pass a reference type by reference.
链接地址: http://www.djcxy.com/p/21010.html上一篇: 调用函数后为什么字符串值保持不变
下一篇: 真的是有价值的传递吗?