After calling func1 variable mydata stays null. In debug mode I see that in func3 it sets data to string. why it doesn’t pass value after exiting function?
Class example
class myclass
{
public string mydata;
public int func1()
{
//....
func2(/**/, mydata);
//....
return 1;
}
private int func2(/**/,data)
{
byte[] arr = new byte[1000];
//...
func3(arr,data);
//...
return 1;
}
private void func3(byte[] arr, string data)
{
char[] a = new char[100];
//...
data = new string(a);
}
}
By default, parameters are passed by value; it means that what is passed is actually a copy of the variable (which is a copy of the reference in the case of a reference type like
string). Whenfunc3assignsdata, it only modifies a local copy of the variable.Now, if you change
func2andfunc3signatures so thatdatais passed by reference, you will get the expected result:I suggest you read Jon Skeet’s article about parameter passing for more details.