Given the following method:
static void ChangeArray(params string[] array) {
for (int i = 0; i < array.Length; i++)
array[i] = array[i] + "s";
}
This works if I call it passing a array of strings:
string[] array = {"Michael", "Jordan"} // will become {"Michaels", "Jordans"}
ChangeArray(array);
But will not work if I call it using string arguments:
string Michael = "Michael";
string Jordan = "Jordan";
ChangeArray(Michael, Jordan); // This will NOT change the values of the variables
I understand that the compiler will wrap Michael and Jordan on an array, so shouldn’t the results be the same on both cases?
Your second example is essentially:
so; actually, the values inside
tmpwere changed… buttmpwas discarded afterwards, so you don’t see anything.paramsdoes not emulateref– it won’t do a position-wise update back into the original variables. Or in code, it is not the following:If you need it to behave like that, then code it like that – or use instead an overload that takes
refparameters.