Why string needs to be passed as ref, even though it is reference type?

Hi,

I want to pass the string value to a function and the changes should refelect in the called function. What I thought was if I pass the string normally (without using ref keyword) this will work since string is a reference type. If we are passing any userdefined class reference it is working. Why it is not working for string even though it is a reference type

Example:

public void fun()

{

String name = "Joe";

MessageBox.Show(name);

change(name);

MessageBox.Show(name);

}

private void change(String address)

{

name = "Tom";

}

The output is,

Joe

Joe

public void fun()

{

String name = "Joe";

MessageBox.Show(name);

change(ref name);

MessageBox.Show(name);

}

private void change( ref String address)

{

name = "Tom";

}

The output is,

Joe

Tom




Answer this question

Why string needs to be passed as ref, even though it is reference type?

  • Bill R.

    I am not sure about your answer. If that is the case why is it allowing to modify the string by using ref. I am not clear about it. You answer doesn't answer my query. Even I thought of the same. But i don't think that is the reason.

  • PeeJayGee

    Well, it is not good style to change values in a method, I assume you are using this in an example.

    To answer your question, strings are immutable. changing a string actually creates a new reference. If you don't pass by reference, the original reference is maintained.

    I am not sure if i am explaining that correctly, but that is the basic idea.

    Now, if you have a set of arguments you are passing to a method, and you are looking to change one or more of the arguments, this calls for a refactoring.

    specifically - "Introduce Parameter Object"

    see Refactoring: Improving the Design of Existing Code (Fowler, 1999 0201485672 )



  • Mike Flasko

    Hi Blair Allen Stark,

    Thank you that does make sense. Thanx alot. Your example was pretty good one.

    With Regards,



  • Willbur4u

    I like to use balloons on tethers as an analogy for reference types.

    lets say, I have a balloon on a tether. . .

    I can attach a second tether to it and had it to you (pass by value - copy of reference - same balloon ) You can detach your tether and attach it to another balloon. And then you and I will reference different ballons. Of you pop yours, mine is still intact. And the moment you let go of your tether, your balloon is lost, but I still have mine.

    But because I passed by value, you cannot change my tether. That means you cannot detach my tether from the balloon.

    Now remember, strings are immutable, so what this means is, because we passed by value, you attached your tether to a new ballon, and my original tether is still pointing at my original balloon.

    Or I can actually hand you my tether (pass by reference - same reference - same balloon) Any change you make to the tether will persist when control is returned to me.

    does that make sense



  • Why string needs to be passed as ref, even though it is reference type?