How to Use the ref parameter in C #

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.