Passing reference type in C #

class Test { static void Func(StringBuilder myString) { myString.Append ("test"); myString = null; } static void Main() { StringBuilder s1 = new StringBuilder(); Func(s1); Console.WriteLine (s1); } } 

The output of "Test", why is it not null?

If s1 is passed by reference in Func (), then why myString.Append("test") changes it, but myString = null does not work?

Thanks in advance.

+8
c #
source share
5 answers

if s1 is passed by reference to Func (), then why myString.Append ("test") changes it

It is not passed by reference; instead, its value is passed to the function.

Suppose that S1 points to memory location 0x48. This will be passed to Func , where the Func parameter myString will begin to point to this location. Later, when you Append enter text in this place, it will be added. But later, when you assign null to myString , it starts to point to nothing, but the original location remains unchanged.

You should see: Passing parameters in C # from Jon Skeet

Consider the following diagrams. In step 1, you create a new object of type StringBuilder and S1 refers to this object.

enter image description here

In step 2, after passing S1 to Func now myString also points to the same object in memory.

enter image description here

Later, when you Append , text through myString , it updates the original object, since myString points to a memory location stored in the object.

In step 3, when you assign null to myString , it loses the address for the StringBuilder object, but it does not change anything in the object or pointer S1 .

enter image description here

+11
source share

You pass an object reference by value.

those. the address of the object is passed by value, but the address of the object and the object is the same. Therefore, when you call your method, VM copies the link; you just change the copy.

In myString.Append you use the copied link to go to the object (still only one object), which is then modified / called.

What do you think you are doing is:

 class Test { static void Func(ref StringBuilder myString) { myString.Append("test"); myString = null; } static void Main(string[] args) { StringBuilder s1 = new StringBuilder(); Func(ref s1); Console.WriteLine(s1); } } 

More detailed explanation: http://javadude.com/articles/passbyvalue.htm

+11
source share

In C #, a pointer (link) to a complex type is passed by value by default. You must indicate that it is passed by reference, for example:

 static void Func(ref StringBuilder myString) { myString.Append ("test"); myString = null; } 
+2
source share

add ref to the function to pass it by reference.

 static void Func(ref StringBuilder myString) { myString.Append ("test"); myString = null; } 

Then name it like this:

 Func(ref s1); 
0
source share

Because you are passing a link to your StringBuilder s1 to Func. Inside Func, a reference to the same StringBuilder instance is called "myString". First tell him to do something (" myString.Append("Test") ), then set the INSIDE Func parameter to null. The result of Append is visible Main (), the link set to null does not affect s1 in Main.

0
source share

All Articles