Difference between ref and out (resend)

Source: Internet
Author: User

First, if you do not use these two keywords, what is that?

See the following example:

Using system;
Class Test
{
Static void swap (ref int X, ref int y)
{
Int temp = X;
X = y;
Y = temp;
}
Static void swap (int x, int y)
{
Int temp = X;
X = y;
Y = temp;
}
Static void main ()
{
Int I = 1, j = 2;
Swap (Ref I, ref J );
Console. writeline ("I = {0}, j = {1}", I, j );
Swap (I, j );
Console. writeline ("I = {0}, j = {1}", I, j );
}
}

After the program is compiled, execute the following output:

I = 2, j = 1
I = 2, j = 1

This is an example of csdn post. In fact, if we put it together, we cannot easily see the difference between using ref and not using ref.

It is obvious to read them separately.

Use Ref:

Using system;
Class Test
{
Static void swap (ref int X, ref int y)
{
Int temp = X;
X = y;
Y = temp;
}


Static void main ()
{
Int I = 1, j = 2;
Swap (Ref I, ref J );
Console. writeline ("I = {0}, j = {1}", I, j );
}
}

After the program is compiled, execute the following output:

I = 2, j = 1

Not used:

Using system;
Class Test
{
Static void swap (int x, int y)
{
Int temp = X;
X = y;
Y = temp;
}
Static void main ()
{
Int I = 1, j = 2;
Swap (I, j );
Console. writeline ("I = {0}, j = {1}", I, j );
}
}

After the program is compiled, execute the following output:

I = 1, j = 2;

Obviously, after calling a function with the ref keyword, the values of I and j have changed, but those without REF have not changed. Why?

Please refer to the following passage:

Method parameters are worth special attention. There are four types of parameter transfer: by value, by reference, output, and array ). There is no additional modifier for the value passing parameter. The address passing parameter requires the modifier ref, the output parameter requires the modifier out, And the array parameter requires the modifier Params. If the value of the parameter is changed during a method call, the parameter passed in to the method does not change after the method call is completed, but retains the original value. On the contrary, if the value of the parameter is changed during the method call process, the parameters for passing in the method also change after the call is completed. In fact, we can clearly see the meaning of the two from the name-the pass-through parameter transfers a copy of the call parameter, while the transfer parameter passes the memory address of the call parameter, this parameter points to the same storage location inside and outside the method.

Therefore, we can see that the function parameter we usually write is a value parameter (no additional modifier is required for the value parameter), whether it is a value type or a reference type. you can try this example (this example is for the phrase "whether it is a value type or a reference type ,):

Using system;
Class Test
{
Static void swap (string X, string y)
{
String temp = X;
X = y;
Y = temp;
}
Static void main ()
{
String I = "1", j = "2 ";
Swap (I, j );
Console. writeline ("I = {0}, j = {1}", I, j );
}
}

After the program is compiled, execute the following output:

I = "1", j = "2 ";

Note: The string type is a reference type.

The value of I and J has not changed, indicating that when the reference type is used as a function parameter, as long as no ref or out is added, then it is still a value parameter (I always thought that the value type is a value parameter when used as a function parameter, while the reference type is an address parameter when used as a function parameter ).

The. NET Framework programming (revision) has the following sentence: the difference between ref and out is parameter initialization and parameter return,

But here, "out" is the output parameter. is adding "out" an address transfer parameter? Otherwise, how can it change? (it should be the same. Maybe it's just a name. It can be understood that the output parameter is also an address passing parameter )?

Using system;
Class Test
{
Static void swap (Out int X, out int y)
{
Int temp = X;
X = y;
Y = temp;
}


Static void main ()
{
Int I = 1, j = 2;
Swap (Out I, out J );
Console. writeline ("I = {0}, j = {1}", I, j );
}
}

After the program is compiled, execute the following output:

I = 2, j = 1

Summary: The pass-through parameter transmits a copy of the call parameter, while the pass-through parameter transmits the memory address of the call parameter, this parameter points to the same storage location inside and outside the method.

This statement makes it clearer: The value passing parameter transmits the copy address of the call parameter. this parameter is not directed to the same storage location inside and outside the method, the address passing parameter transmits the memory address of the calling parameter. This parameter points to the same storage location inside and outside the method,

Understand this sentence and naturally understand why it changes or why it does not.

The above is my personal understanding, and there must be something wrong with it. Please criticize and point it out.

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.