The difference between out and ref in C #

Source: Internet
Author: User

First: Both are passed by address and will change the value of the original parameter after use.

Second: Ref can pass the value of the parameter into the function, but out is to empty the parameters, that is, you can not pass a value from out into the, out into the value of the parameter is empty, so you have to initialize it once. This is the difference of two, or as some netizens say, ref is in there, out is only out.

Ref (C # Reference)

The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes to the parameters in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword.

For example:

Class refexample{    static void Method (ref int i)    {        i =;    }    static void Main ()    {        int val = 0;        Method (ref val);        Val is now,}    }

The arguments passed to the ref parameter must be initialized first. This differs from out, whose arguments do not need to be explicitly initialized before being passed.

Although ref and out are handled differently at run time, they are handled the same way at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical, so the following code will not be compiled:

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

However, if a method takes a ref or out parameter and the other method does not take both parameters, it can be overloaded, as shown in the following example:

Class refoutoverloadexample{public    void SampleMethod (int i) {}    

Out (C # Reference)

The out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword.

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 method needs to be called to assign a value before the method returns.

The ref and out keywords are handled differently at run time, but are handled the same way at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical, so the following code will not be compiled:

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 a method takes a ref or out parameter and the other method does not take these two types of arguments, it can be overloaded as follows:

Class refoutoverloadexample{public    void SampleMethod (int. i) {} public    void SampleMethod (out int i) {}}

Here are some of my experiences:

Difference:

The difference between ref and out is that in C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword. The two keywords, ref and out, can provide similar effects and act like pointer variables in C. The difference between them is:

1. When using a ref parameter, the passed-in parameter must first be initialized. For out, initialization must be done in the method.

2. When using ref and out, the ref or out keyword is added to the method's parameters and execution methods. To meet the match.

3. Out is suitable for use where multiple return values need to be retrun, while ref is used to modify the caller's reference in a method that needs to be called.

Out

The Out method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.

Declaring an out method is useful when you want a method to return more than one value. Methods that use out parameters can still return a value. A method can have more than one out parameter.

To use an out parameter, you must explicitly pass the parameter to the method as an out parameter. The value of the out parameter is not passed to the out parameter.

You do not have to initialize a variable passed as an out parameter. However, you must assign a value to the out parameter before the method returns.

property is not a variable and cannot be passed as an out parameter.

Ref is there in and out, while out is only out of step.

The difference between out and ref in C #

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.