System.String - Reference Type
-
in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.?
private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }
-
in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.?
private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }
-
in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.?
private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }
If you pass an object of a reference type to a method, all modifications to the method parameter object will affect the original object. For example changing the
Text
property of aTextBox
. That doesn't include changing the reference itself (this is what you do). If you want this to affect your original object you have to use either theout
orref
method parameter keyword.
-
in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.?
private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }
From what I am aware of a System.String is a reference type, yes, but it's also immutable - i.e., the character sequence in it cannot be changed once it is created. Methods that appear to change a string only return a new string with the modifications made. Note: everything below here is my interpretation of what is going on. :) When you do something like this:
string a = "one";
string b;b = a;
a and b will point to the same location for the string in memory - just like you expect with reference types. The catch is that because a string is immutable, as soon as you do this:
b = "two";
The CLR will now remove b's reference to a, create a new String object (with the character sequence "two") and assign that to b. a and b now point to two entirely different System.String's in memory. This process of sharing a reference until someone makes a change helps keep memory usage down whilst keeping a System.String inherantly immutable - two strings will point to each other until one of them changes. When one of them changes, two entirely seperate strings are created. Even when you pass a string by reference (using
ref
) you still aren't actually modifying the original string. All you are doing is creating a new System.String in memory somewhere and changing the ref'd variable's reference to point to this new string. The old string still exists in memory somewhere, but providing certain conditions are met it's up for garbage collection now.public void Foo() { string one="two"; Bar(ref one); // pass in the refence to the variable "one". Does not pass the string. } public void Bar(ref string inStr) { // create a new System.String in memory, set it to the character // sequence "three", and change inStr's reference to point to this new string. inStr = "three"; }
This space for rent! My Blog