The ref parameter descriptions in the books on the market are generally as follows:
The ref variable value is changed during use, and the example is as follows:
Public static void valueparam (string Str)
{
STR = "251 ";
}
Public static void refparam (ref string Str)
{
STR = "250 ";
}
Public static void main ()
{
String STR = "249 ";
Valueparam (STR );
Console. writeline ("value Param:" + Str );
Refparam (ref Str );
Console. writeline ("Ref Param:" + Str );
}
Result:
Value Param: 249
Ref Param: 250
However, in practice, I found that the value of the array parameter will change without modifying the ref parameter. See the following example:
Public void chagevalue (INT [] ARR)
{
For (INT I = 0; I <arr. length; I ++)
Arr [I] = 0;
}
Public static void main ()
{
Int [] AAR = {3, 4, 5, 7, 2, 6, 1 };
For (INT I = 0; I <AAR. length; I ++)
Console. writeline (">>>" + AAR [I]. tostring ());
Chagevalue (AAR); // The ref parameter is not used here.
For (INT I = 0; I <AAR. length; I ++)
Console. writeline ("<" + AAR [I]. tostring ());
}
From the results, we can see that the array itself has changed, so we can not only change the parameter value in the form of ref, but this situation is not introduced in general books, I hope that the new book about C # will explain the above situation.
Thank you.