-1
class Program
{
    static void Method(out int i, out string s1, out string s2)
    {
        i = 44;
        s1 = "I've been returned";
        s2 = null;
    }
    static void Main()
    {
        int value;
        string str1="fwer", str2;
        Method(out value, out str1, out str2);
        Console.WriteLine (str1);
        Console.ReadKey();
        // value is now 44
        // str1 is now "I've been returned"
        // str2 is (still) null;
    }

In the above I am initializing str1 and sending the str1 as out and changing the value in the caller method. as string is immutable. changing it should create a new string object. but how it's printing the value correctly in the calling method which changed in caller method.

thanks in advance.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
user1844634
  • 1,221
  • 2
  • 17
  • 35
  • have you tried creating this in .net fiddle to see what would happen? – Callum Linington Oct 02 '15 at 07:55
  • really try to avoid using the out parameter as much as possible since it is considered bad practice and not so much clear or intuitive. you should be heading more for an OO approach (i.e returning an object with the desired result). look here for the definition of the out parameter:https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx. take a look here, an excellent answer by Jon Skeet: http://stackoverflow.com/questions/810797/which-is-better-return-value-or-out-parameter – Ahmed ilyas Oct 02 '15 at 07:57
  • 1
    It's difficult to know what you're looking for here without more context. Is this "I understand `out` parameters in *all* situations, except when it's a `string`"? Or "what is an `out` parameter?" Or something else in between? – Damien_The_Unbeliever Oct 02 '15 at 07:57
  • 1
    Did you read this https://msdn.microsoft.com/en-us/library/ee332485.aspx? The last example uses string out parameters. – Henrik Oct 02 '15 at 07:57
  • Would've been quicker to google it. –  Oct 02 '15 at 08:02
  • i didn't get exact answer after googlig. as a last option i posted question here – user1844634 Oct 02 '15 at 08:05
  • string with out is bit a tricky concept. If it's int,float etc with out it's a straight forward. AS string is immutable using it with out makes this question interesting, looking for some good detailed answers – user1844634 Oct 02 '15 at 08:07

2 Answers2

1

string is immutable, but you aren't changing the contents of a string. You are creating a new string and replacing a string with that new one.

This code:

string str1="fwer", str2;
Method(out value, out str1, out str2);

Is doing the equivalent of this:

string str1 = "fwer", str2;
str1 = "I've been returned";
str2 = null;

When you use an out parameter, it passes the argument by reference.

Since a string is already a reference type, this means that a reference to a reference is passed. Then when you change it inside the method, you are changing a reference and not the contents of the string.

Consider the initial state of str1 where it references a string object.

str1 --> ["fwer"]

When you pass it to the method as parameter out string s1 this is what happens:

s1 --> str1 --> ["fwer"]

So s1 is referencing the str1 reference, and changing it will change what str1 references.

Then this line of code:

s1 = "I've been returned";

Makes the item referenced by s1 reference the string "I've been returned":

s1 --> str1 --> ["I've been returned"]
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

String is a reference type with value type semantics. Strings are immutable. When you pass strings as an out/ref function argument it is no different than pass any other reference type.

A sample to clarify the situation:

void Foo()
{
 string str; //reference to string is created which points to null
 str = "foo";//string object is created and "str" is now points to it   

 Bar(out str);//pass "str" as a reference to reference
}

void Bar(out string str1) //"str1" is like a pseudomin for "str". If you change "str1" the "str" changes to.
{
 str1 = "bar";//new string object is created and both "str1" and "str" pointed to it
}

to your updated question:

string s1 = "foo"; 
string s2 = s1;

There are two references to string and single string object.

s1, s2 - references. "foo" is the object.

References can be changed, "foo" can't.

Immutability means that there is no such method like:

s1.SomeMethodChangesStringInnerState();
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
mt_serg
  • 7,487
  • 4
  • 29
  • 45