I used to have this confusion about what exactly happens when reference types are passed by value to functions till recently. Let me illustrate what I have learnt since then with a few examples:
static void Main(string[] args)
{
string s = "UNITED";
ChangeString(s);
Console.WriteLine(s);
StringBuilder a = new StringBuilder("UNITED");
ChangeSb(a);
Console.WriteLine(a);
Console.Read();
}
private static void ChangeString(string s1)
{
s1 += "STATES";
}
private static void ChangeSb(StringBuilder a1)
{
a1.Append("STATES");
}
Output:
UNITED
UNITEDSTATES
Explanation:
So reference types always contain the memory location of the data and not the actual data itself. In the above example s will contain the memory location where 'United' is stored . And then when ChangeString(s) the new variable s is assigned the same memory location that contains 'United'. However when 'States' is appended to it, since string are immutable, a new string is created which contains the memory location of the word 'States'. Therefore, when we output s we still get 'United' since the original variable is untouched. For this example, lets assume the entire word is stored at that memory location because it is not important.
In the case of the StringBuilder a, it again points to the memory location where 'United' is stored. When ChangeSb(a) is called, the new StringBuilder contains the memory location where 'United' is stored.
When "States" is appended to it, the memory location will now contain "United States". A new location is not used since StringBuilders are not immutable.
And another example:
static void Main(string[] args)
{
string s = "UNITED";
ChangeString(s);
Console.WriteLine(s);
StringBuilder a = new StringBuilder("UNITED");
ChangeSb(a);
Console.WriteLine(a);
Console.Read();
}
private static void ChangeString(string s)
{
s = "STATES";
}
private static void ChangeSb(StringBuilder a)
{
a = new StringBuilder("STATES");
}
Output:
UNITED
UNITED
static void Main(string[] args)
{
string s = "UNITED";
ChangeString(s);
Console.WriteLine(s);
StringBuilder a = new StringBuilder("UNITED");
ChangeSb(a);
Console.WriteLine(a);
Console.Read();
}
private static void ChangeString(string s1)
{
s1 += "STATES";
}
private static void ChangeSb(StringBuilder a1)
{
a1.Append("STATES");
}
Output:
UNITED
UNITEDSTATES
Explanation:
So reference types always contain the memory location of the data and not the actual data itself. In the above example s will contain the memory location where 'United' is stored . And then when ChangeString(s) the new variable s is assigned the same memory location that contains 'United'. However when 'States' is appended to it, since string are immutable, a new string is created which contains the memory location of the word 'States'. Therefore, when we output s we still get 'United' since the original variable is untouched. For this example, lets assume the entire word is stored at that memory location because it is not important.
In the case of the StringBuilder a, it again points to the memory location where 'United' is stored. When ChangeSb(a) is called, the new StringBuilder contains the memory location where 'United' is stored.
When "States" is appended to it, the memory location will now contain "United States". A new location is not used since StringBuilders are not immutable.
static void Main(string[] args)
{
string s = "UNITED";
ChangeString(s);
Console.WriteLine(s);
StringBuilder a = new StringBuilder("UNITED");
ChangeSb(a);
Console.WriteLine(a);
Console.Read();
}
private static void ChangeString(string s)
{
s = "STATES";
}
private static void ChangeSb(StringBuilder a)
{
a = new StringBuilder("STATES");
}
Output:
UNITED
UNITED
No comments:
Post a Comment