Keyword ref and out in C #

Source: Internet
Author: User

Q: Why does ref and out exist in C? (Not in Java)
Requirement hypothesis
: Now we need to exchange the values of the two variables A and B through a method called swap. A = 1, B = 2 before exchange, asserted: After exchange, A = 2, B = 1.

The encoding is as follows:

Class Program
{
Static void main (string [] ARGs)
{
Int A = 1;
Int B = 2;
Console. writeline ("before switching \ TA = {0} \ TB = {1} \ t", a, B );
Swap (A, B );
Console. writeline ("after switching \ TA = {0} \ TB = {1} \ t", a, B );
Console. Read ();
}
// Exchange the values of two variables, A and B.
Private Static void swap (int A, int B)
{
Int temp =;
A = B;
B = temp;
Console. writeline ("Method \ TA = {0} \ TB = {1} \ t", a, B );
}
}

Running result:

Exchange before a = 1 B = 2

Method A = 2 B = 1

After the switch, a = 1 B = 2

Assertion failed, not meeting our needs!

Cause Analysis: int type is a value type, which exists in the thread stack. When the swap (a, B) method is called, it is equivalent to copying the values of A and B (I .e., 1 and 2), and then exchanging the two values in the method. After the exchange, A is still the original A, B is the original B. This is the principle of passing values in C #. It transfers a copy of the data corresponding to the variable instead of a reference.

Solution: Therefore, the ref and out keywords are proposed in C.

Modify the Code as follows to achieve the desired result:

Class Program
{
Static void main (string [] ARGs)
{
Int A = 1;
Int B = 2;
Console. writeline ("before switching \ TA = {0} \ TB = {1} \ t", a, B );
Swap (Ref A, ref B );
Console. writeline ("after switching \ TA = {0} \ TB = {1} \ t", a, B );
Console. Read ();
}
// Exchange the values of two variables, A and B.
Private Static void swap (ref int A, ref int B)
{
Int temp =;
A = B;
B = temp;
Console. writeline ("Method \ TA = {0} \ TB = {1} \ t", a, B );
}
}

Similarly, using out can also meet our needs.

Let's talk about the differences between ref and out:

1. About heavy load

Principle: methods with the Out | ref keyword can be overloaded with methods without the out and ref keywords. to overload the out and ref keywords, the compiler will prompt: you cannot define method overloading only on ref and out.

2. Initial values before a call

Principle: Before calling a function as a parameter, the ref parameter must be assigned an initial value. Otherwise, the compiler will prompt that the unassigned local variable is used;

Before calling an out function as a parameter, the real parameter can not be assigned an initial value.

3. Questions about initial parameter values introduced in the function

Principle: In the called function, the out parameter is assigned a value at least once before the return value. Otherwise, the compiler will prompt that the out parameter is not assigned a value;


In the called function, parameters introduced by ref do not need to be assigned an initial value before return.

Summary:In C #, ref and out provide a solution for passing value types by reference. Of course, the reference types can also be modified using ref and out, but this has no meaning. Because the reference data type is a copy of the passed reference rather than a value. The ref and out keywords tell the compiler that the parameter address is passed instead of the parameter itself, which is the same as the default transfer method of the reference type. At the same time, the compiler does not allow overload between the out and the ref. It also fully demonstrates that the difference between the out and the ref is only from the compiler's perspective, and the Il code they generate is the same. Some may wonder why the value type will not allocate memory in the managed heap. why can it be passed by address? Although the value type lives in the thread stack, it represents the data itself (unlike the referenced data type itself, it does not represent the data but points to a memory reference ), however, the value type also has its own address, that is, the pointer. After modifying it with ref and out, the pointer is passed. Therefore, after, the value of B is truly exchanged. This is the benefit of ref and 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.