Parameters are passed by value by default. If you pass a parameter into a method, the original variable does not get modified. If you pass a parameter as a ref
parameter though, the original variable that you passed in can get modified.
Try this:
public void Func1(String abc) {
abc = "Changed from Func1";
}
public void Func2(ref String abc) {
abc = "Changed from Func2";
}
public void main() {
string foo = "not changed";
Func1(foo);
Console.WriteLine(foo);
Func2(ref foo);
Console.WriteLine(foo);
}
The output you will get is:
not changed
Changed from Func2
In Func1
a copy of foo
is created, which refers to the same String. But as soon as you assign it another value, the parameter abc
refers to another String. foo
is not modified and still points to the same string.
In Func2
you pass a reference to foo
, so when you assign abc
a new value (i.e. a reference to another string), you are really assigning foo
a new value.