If I understand correctly, you want the variables my
and test.myStr
to be linked, so if one changes, the other changes?
The answer is simple: It cannot!
A string is an immutable class. Multiple references can point to a string instance, but once this instance is modified, a string instance is created with the new value. So a new reference is assigned to a variable, while the other variables still point to the other instances.
There is a workarounds, but I suspect you will not be happy with it:
public class Test
{
public string mystr;
}
Test myTest1 = new Test { myStr = "Hello" };
Test myTest2 = myTest1;
Now, if you change myTest1.myStr
, the variable myTest2.myStr
will also be modified, but that is simply because the myTest1
and myTest2
are the same instances.
There are other solutions like these, but the all come down to the same aspect: A class holding a reference to a string.