The difference between out and ref collation

Source: Internet
Author: User

Both ref and out are keywords in C #, and the functionality is similar to specifying that a parameter is passed by reference.

For compiled programs, there is no difference between them, that is, they only have syntax differences.

To sum up, they have the following grammatical differences:

1, the parameters passed in ref must be initialized before the call, out of the need, that is:
int i;
SomeMethod (ref i);//Syntax error
SomeMethod (out i);//Through

2, ref passed in the parameters can be used directly inside the function, and out cannot:
public void SomeMethod (ref int i)
{
int j=i;//through
//...
}
public void SomeMethod (out int i)
{
int j=i;//Syntax error

}


3, the parameters passed in ref can not be modified inside the function, but out must be assigned before leaving the function body.

Ref must be initialized before the parameter is passed, and out is not initialized before it is passed, and the conversion process between a value type and a reference type is called boxing and unboxing.


Summarize:
It should be said that the system has less restrictions on ref. Out although it is not required to be initialized before the call, its value is not visible inside the function, that is, the value passed through out cannot be used, and it must be assigned a value within the function. Or, the function assumes responsibility for initializing this variable.

Let's talk about what the difference between ref and Out is:

1 About overloading

Principle: Methods with Out|ref keywords can be overloaded with methods that do not have out and ref keywords, but if you want to overload between out and ref, the compiler will prompt: You cannot define method overloads on ref and out only

2 about pre-call initial values

Principle: a function with ref as a parameter must have an initial value assigned before the call. Otherwise, the compiler will prompt that the unassigned local variable is used;

An argument can not be assigned an initial value until the function is called.

3 about the parameter initial value problem that is introduced within the function

Principle: Within the called function, the parameters that are introduced by the out are assigned at least once before they are returned, otherwise the compiler will prompt: The unassigned out parameters are used;

Within the called function, the parameter introduced by ref does not have to be assigned an initial value before it is returned.

Summary: Ref and Out in C # provide solutions that value types are passed by reference, and of course reference types can be decorated with ref and out, but that has lost meaning. Because the reference data type is inherently a copy of the reference itself rather than the value. The ref and out keywords tell the compiler that the address of the parameter is now passed instead of the parameter itself, which is passed the same way as the reference type by default. At the same time, the compiler does not allow overloads between out and ref, and fully illustrates that the difference between out and ref is only the compiler angle, and they generate the same IL code. Some people may wonder, as I have just started to study, the same confusion: value types do not allocate memory in the managed heap, why can they be passed by address? Although the value type represents the data itself in the stack of a live thread (unlike the reference data type itself does not represent data but points to a memory reference), the value type also has its own address, that is, the pointer, which is now passed with ref and out, is the pointer, so you can implement the modified A, The value of B is actually exchanged. This is the benefit that ref and out bring to us.

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

Second: Rel can pass the value of the parameter into the function, but out is to empty the parameters, that is, you can not transfer a value from out into the, out into the parameter value is empty, so you have to initialize it once. This is the two difference, or as some netizens said, Rel 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 = 44;

}

static void Main ()

{

int val = 0;

Method (ref val);

Val is now 44

}

}

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 out ".

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) {}

public void SampleMethod (ref 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.

For example:

Class Outexample

{

static void Method (out int i)

{

i = 44;

}

static void Main ()

{

int value;

Method (out value);

Value is now 44

}

}

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 out ".

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) {}

}

The difference between out and ref collation

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.