C #, the difference between out and ref

Source: https://www.cnblogs.com/sunliyuan/p/5999045.html

First of all, both those who are passed by address, after use will change the value of the original parameters.

Secondly, ref value of the parameter can be passed into the function, but is put out parameter empty, you can not put a value that is passed from the inside out, and go after out, the value of the parameter is empty, so you must be initialized once. This is both a difference, or as some friends say - ref is a carry out there, out is out without.

ref (C # Reference)

the ref keyword parameters passed by reference. The effect is that when control passes back to the calling method, any changes in the method parameters will be reflected in the variable. To use a ref parameter, the method definition and the calling method must explicitly use the ref keyword. E.g:

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 first be initialized. This is different with out which parameters need not be explicitly initialized before transmission.

Despite the different ref and out process at runtime, but the same approach at compile time. Thus, if a method using the ref parameter, and another method using out parameter can not be overloaded maybe methods. For example, from the viewpoint of the compiler, the following two code method is identical, and therefore will not compile the following code:

public void SampleMethod(ref int i) { }
public void SampleMethod(out int i) { }

However, if a method using the ref or out parameters, other methods maybe employed without parameters, may be overloaded, the following example:

public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }

 

out (C # Reference)

out keywords will lead to parameters passed by reference. This is similar to the ref keyword, except that ref requires variable must be initialized before delivery. To use out parameters, method definition and the calling method must explicitly use the out keyword.

class OutExample
{
    static void Method(out int i) {
        i = 44;
    }
    static void Main(){
        int value;
        Method(out value);
        // value is now 44    
    }
}    

Although the variables are initialized before passed as parameters do not have to pass out, but you need to call methods for assigned before the method returns.

 

The following are some of my experiences:

the difference:

Ref and out of the difference in C #, may be either a value passed by reference. Passed by reference parameter allows to change the value of the parameter a function member, and holds the change. To pass parameters by reference, use the ref or out keyword. maybe ref and out keywords are able to provide a similar effect, the effect is very similar in the C pointer variable. Their difference is:

  1. When using the ref-type parameters, the parameters passed must be initialized. On out, it must complete its initialization process.
  2. When using the ref and out, when the parameters and methods of execution, Ref or Out keywords will be added to meet the match.
  3. out fit where you need to return multiple return values, and the ref is used in the method needs to be called when referring to modifications of the caller.

out:

out method to use keywords on the method parameters passed by reference to the method of the same variable. When control passes back to the calling method, any changes made in the method parameters are reflected in the variable.

When you want a method to return multiple values, declared out method is useful. Out parameters method can still return a value. A method can have more than one out parameter.

Not necessary to initialize variables passed as out parameters. However, it must be assigned before the method returns to the out parameter.

Attribute is not a variable, not as an out parameter.

 

Guess you like

Origin www.cnblogs.com/xulinjun/p/11456002.html