You are assigning reference and not modifying the original reference.
When you assign A.field
to a
you assign reference of string "1"
to a
. Later you assign a new string to A.field
and there was no change in the original value of a
. It still holds the old reference to string "1"
.
If somehow, you could modify the original reference then you should be able to see the change. Since your type is string, you can't really modify it because it is immutable, but consider an example with StringBuilder
.
public static class A
{
public static StringBuilder field { get; set; } = new StringBuilder("1");
}
And later.
static void Main(string[] args)
{
var a = A.field;
A.field.Insert(0, "2");
Console.WriteLine(a);
Console.Read();
}
Now you will get modified value "21"
in variable a
as well.
Notice that the code above is modifying the field with A.field.Insert(0, "2");
and since variable a
holds the reference to A.field
, you see the change on the next line.
But if you try to assign a new reference to A.field
with statement like:
A.field = new StringBuilder("2");
Then A.field
will have a new object to reference, and the previous variable a
will still keep the old reference.