Is really argument passing by value?

This question already has an answer here:

  • C# pass by value vs. pass by reference 6 answers
  • Passing Objects By Reference or Value in C# 7 answers

  • 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:

  • You buy a new apartment ( new StringBuilder() ) and you get a key ( s ) that grants you access.
  • You hire a cleaner ( s.AppendLine ) and you give him a copy of your key so he can access your apartment and clean it up.
  • The cleaner uses the copy of your key, cleans up your apartment and, because he feels like it, reprograms the key to open some other apartment.
  • You finish your daily work and come back home. Your key opens the door to your apartment just fine and you find it clean.
  • That's basically what happens when you pass a reference type by value.

    Now consider the following:

  • You buy a new apartment ( new StringBuilder() ) and you get a key ( s ) that grants you access.
  • You hire a cleaner ( s.AppendLine ) and you give him your key so he can clean up your apartment.
  • The cleaner cleans up your room and, becauase he feels like it, reprograms the key you gave him to open some other apartment. He leaves your key under the door mat.
  • You finish your daily work and come back home. You try to use your key but the door wont open. Through a window you can see your apartment is clean.
  • Thats what happens when you pass a reference type by reference.

    链接地址: http://www.djcxy.com/p/21010.html

    上一篇: 调用函数后为什么字符串值保持不变

    下一篇: 真的是有价值的传递吗?