Ref (C # reference)

Source: Internet
Author: User

RefKeyword to pass Parameters by reference. The effect is that when the control is passed back to the call method, any changes made to the parameters in the method will be reflected in the variable. To use
RefParameter, the method definition and call method must be explicitly usedRefKeyword. For example:

Other
class RefExample{    static void Method(ref int i)    {        i = 44;    }    static void Main()    {        int val = 0;        Method(ref val);        // val is now 44    }}

PassRefThe parameter must be initialized first. Unlike out, out parameters do not need to be explicitly initialized before being passed. (See
Out .)

AlthoughRefAndOutThe processing methods are different at runtime, but they are the same at compilation. Therefore, if a method uses
RefParameter, while the other method uses
The out parameter cannot be used to reload the two methods. For example, from the compilation perspective, the two methods in the following code are identical, so the following code is not compiled:

Other
class CS0663_Example {    // compiler error CS0663: "cannot define overloaded     // methods that differ only on ref and out"    public void SampleMethod(ref int i) {  }    public void SampleMethod(out int i) {  }}

However, if a method usesRefOr
The out parameter, and the other method does not use these two types of parameters, You can overload it, as shown below:

Other
class RefOutOverloadExample{    public void SampleMethod(int i) {  }    public void SampleMethod(ref int i) {  }}
Note:

Attribute is not a variable, so it cannot be used RefParameter transfer.

For information on passing arrays, see passing Arrays Using ref and out.

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.