Behold.. my awesome paint skillz:
(Click here for the full image)
When f1
is used within the method, it is a new reference that points to the same object in memory. When you re-assign the reference with f1 = new MyClass();
, you are effectively saying "okay, this reference now points to a new instance". This leaves a1
untouched.. so when the function unwinds and hits the Console.WriteLine
, your a1
reference still references the old area of memory.. which now has a property set at 50.
To have the behaviour you expect, you pass the object in as a ref
parameter:
static void RefAsParameter(ref MyClass f1)