Out and ref parameter modifiers

Source: Internet
Author: User

Organize from MSDN

Out

out   keywords pass parameters by reference. ref keyword, except that ref requires that the variable being initialized befo Re it is passed. " This is similar to the  ref  keyword, except   ref   requires that a variable be initialized before it is passed. out   parameters, both the method definition and the calling method must explicitly use   out   keywords. example:

Class outexample{    static void Method (out int i)    {        i =;    }    static void Main ()    {        int value;        Method (out value);        Value is now,}    }

Although a variable passed as an out parameter does not have to be initialized before it is passed, the calling method still requires a value to be assigned before the method returns .

although   ref   and   out   keywords cause different run-time behavior, and they are not considered part of a compile-time method signature. So if the only difference is that one method takes   ref   parameter, and the other method takes   out   parameter, the two methods cannot be overloaded. For example, the following code would not compile:

Class cs0663_example{    //Compiler error CS0663: "Cannot define overloaded     //methods that differ only on ref and O UT ".    public void SampleMethod (out int i) {} public    void SampleMethod (ref int i) {}}

However, if one method takes a ref or out parameter, and another method takes another parameter, you can complete the overload, such as:
Class outoverloadexample{public    void SampleMethod (int. i) {} public    void SampleMethod (out int i) {i = 5;}}
property is not a variable and therefore cannot be passed as an out parameter.

Ref

ref  keyword causes a parameter to be passed by reference, rather than by value.   For example, if the caller passes a local variable expression or an array element access expression, the calling method replaces the object with the object referenced by the ref parameter, and then the caller's local variable or array element will begin referencing the new object.

ref   Parameters, both the method definition and the calling method must explicitly use   ref   keyword, as shown in the following example.

Class refexample{    static void Method (ref int i)    {        //Rest the mouse pointer over I-Verify that it's an int .        The following statement would cause a compiler error if I        //were boxed as an object.        i = i + +;    }    static void Main ()    {        int val = 1;        Method (ref val);        Console.WriteLine (val);        Output:45    }}

passed to the  ref  parameter must be initialized before it can be passed . out   parameter, and you do not need to explicitly initialize the argument of the parameter until it is passed. out. " > For more information, see  out.

A member of a class cannot have a signature that differs only in the ref and out aspects. if the only difference between the two members of a type is that one has a ref parameter and the other has an out parameter, a compilation error occurs.

Out and ref parameter modifiers

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.