C # parameter keyword ref

Source: Internet
Author: User

The ref keyword allows the method to be passed by reference. The effect is that when the control is passed to the call method, any modifications made to the parameters in the method will be reflected in the change volume.

Example:

Int I =
10;
// View the value before calling the Method
Console. writeline ("before the method calling: I = {0}", I );

Useref (Ref I );

// View the value after the method is called
Console. writeline ("after the method calling: I = {0}", I );
Console. Readline ();

Public static
Void useref (ref
Int I)
{
I + = 100;
Console. writeline ("I = {0}", I );
}

 

The result is as follows:

From the above code, we can see that:

1. To use ref
Parameter, the method definition and call method must be explicitly usedRefKeywords

2. Pass to ref
The parameter must be initialized first.

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.