Ref out keyword usage and differences

Source: Internet
Author: User

The ref keyword allows the parameter to be passed by reference. The effect is that when the control is passed back to the call method, any changes to the parameters in the method will be reflected in this variable. To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods. For example:

Class refoutoverloadexample
{
Public void samplemethod (int I ){}
Public void samplemethod (ref int I ){}
}

 
Passing value types by reference (as shown before this topic) is useful, but ref is also useful for passing reference types. This allows the called method to modify the object referenced by the reference, because the reference itself is passed by reference. The following example shows that the object itself can be changed when the reference type is passed as the ref parameter.

Class refrefexample
{
Static void method (ref string S)
{
S = "changed ";
}
Static void main ()
{
String STR = "original ";
Method (ref Str );
// STR is now "changed"
}
}

 

Parameter transfer can pass the value, and the reference is passed.
All reference types are passed by reference, that is, modifications to the passed variables will be reflected in the original variables,
The value type is passed when no out or ref is used, that is, the modifications to the passed variables are not reflected in the original variables, but only a copy of the original variables.
You can add out or ref to transfer a value type reference.

 

The out keyword causes the parameter to be passed through reference. This is similar to the ref keyword, except that the ref requires that the variables must be initialized before being passed. To use the out parameter, the out keyword must be explicitly used for method definition and call. For example:

Class cs0663_example
{
// Compiler error cs0663: "cannot define overloaded
// Methods that differ only on ref and out ".
Public void samplemethod (Out int I ){}
Public void samplemethod (ref int I ){}
}

 

Although the variables passed as the out parameter do not need to be initialized before being passed, you need to call a method to assign values before the method returns.

The ref and out keywords are processed in different ways at runtime, but are processed in the same way at compilation. Therefore, if one method uses the ref parameter while the other method uses the out parameter, the two methods cannot be reloaded. For example, from the compilation perspective, the two methods in the following code are identical, so the following code is not compiled:

Class cs0663_example
{
// Compiler error cs0663: "cannot define overloaded
// Methods that differ only on ref and out ".
Public void samplemethod (Out int I ){}
Public void samplemethod (ref int I ){}
}

 

However, if one method uses the ref or out parameter, and the other method does not use the two parameters, you can perform the overload operation, as shown below:

Class refoutoverloadexample
{
Public void samplemethod (int I ){}
Public void samplemethod (Out int I ){}
}

The ref keyword allows the parameter to be passed by reference. The effect is that when the control is passed back to the call method, any changes to the parameters in the method will be reflected in this variable. To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods. For example:

Class refexample
{
Static void method (ref int I)
{
I = 44;
}
Static void main ()
{
Int val = 0;
Method (ref Val );
// Val is now 44
}
}

 

Parameters passed to the ref parameter must be initialized first. This is different from out. The latter parameter does not need to be explicitly initialized before being passed. While ref and out are processed differently at runtime, they are processed in the same way during compilation. Therefore, if one method uses the ref parameter while the other method uses the out parameter, the two methods cannot be reloaded. For example, from the compilation perspective, the two methods in the following code are identical, so the following code is not compiled:

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 one method uses the ref or out parameter, and the other method does not use the two parameters, you can perform the overload operation, as shown in the following example:

Class refoutoverloadexample
{
Public void samplemethod (int I ){}
Public void samplemethod (ref int I ){}
}
Author: peng lovely
Source: http://dupeng0811.cnblogs.com/

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.