I know reference types passed to a method can be modified. But I found that it only takes effects on the parameter's members, not the param itself. Here is a test:
That is a reference type:
class MyClass
{
public int PropInt { get; set; }
public string PropStr { get; set; }
public int FieldInt;
public string FiledStr;
}
This method modifies its members:
void MyFun(MyClass myClass)
{
myClass.PropInt = 3;
myClass.FieldInt = 4;
myClass.PropStr = "c";
myClass.FiledStr = "d";
}
This modifies itself(null or new):
void MyFunNull(MyClass myClass)
{
myClass = new MyClass();
//myClass = null;
}
Initializing:
MyClass myClass = new MyClass
{
PropInt = 1,
FieldInt = 2,
PropStr = "a",
FiledStr = "b"
};
Test:
---> output {1, 2, "a", "b"}
MyFun(myClass); ---> output {3, 4, "c", "d"}
MyFunNull(myClass); ---> output {3, 4, "c", "d"}
How is that happening?
do I have to use out/ref in this case?
MyFunbut notMyFunNull