how come passing reference type by ref does not always modify the argument?

This is a discussion on how come passing reference type by ref does not always modify the argument? within the C# Programming forums, part of the General Programming Boards category; I was taught that when calling a method with a reference type as an argument, any changes made to the ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    how come passing reference type by ref does not always modify the argument?

    I was taught that when calling a method with a reference type as an argument, any changes made to the parameter in the method effectively modify the reference type that was used as an argument when calling the method.

    In the case of data types, I was taught that the only way to implement a similar functionality is by using the ref keyword in the method before the parameter and in the method call before the argument.

    That's straightforward.

    But I am confused about instances when calling a method with a reference type argument, and modifying the parameter in the method, does not modify the argument.

    Study the following sample code:

    Code:
    namespace PassingByRef
    {
        class Box
        {
            private string _s;
    
            public string S
            {
                set { _s = value; }
                get { return _s; }
            }
    
            public Box(string s)
            {
                this._s = s;
            }
        }
        
        class Program
        {
            static void Main(string[] args)
            {
                Box box = new Box("Hello");
                Console.WriteLine(box.S);//output: Hello
    
                Test1(box);
                Console.WriteLine(box.S);//output: Hello
    
                Test2(box);
                Console.WriteLine(box.S);//output: Hello???
    
                Test3(ref box);
                Console.WriteLine(box.S);//output: Hello!!!
    
                Console.ReadKey();
            }
    
            static void Test1(Box x)
            {
                x = new Box("Hello!!!");
            }
    
            static void Test2(Box x)
            {
                x.S = "Hello???";
            }
    
            static void Test3(ref Box x)
            {
                x = new Box("Hello!!!");
            }
        }
    }
    In the case of the Test1 method, I'd like to understand why the moment the parameter was initialized "the reference was lost," whereas the reference was preserved in Test2 when only the field of the parameter was modified.

    Also related to the observation concerning Test1, how come the only way to achieve the desired functionality (Test3 method) is by using the ref keyword before a reference type parameter/argument? I had imagined it was redundant.
    Last edited by y99q; 09-24-2011 at 12:15 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We pretty much covered that topic a couple of days ago here: Object passed to method as parameter


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-06-2010, 03:18 AM
  2. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 04:23 PM
  3. passing argument from incompatible pointer type
    By bhdavis1978 in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 01:42 PM
  4. passing argument from incompatible pointer type
    By bgalin in forum C Programming
    Replies: 2
    Last Post: 12-06-2009, 07:14 AM
  5. passing an object as an argument to a function (by reference)
    By odysseus.lost in forum C++ Programming
    Replies: 5
    Last Post: 12-15-2005, 12:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21