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.

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

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 .

Habib
source share