Ref and out (msdn)

Source: Internet
Author: User
Ref and out (msdn)
Release date:Source: plwwwAuthor:

In general:

We usually pass the value to the method. the method obtains a copy of these values, and then uses these copies. After the method is run, these copies will be discarded, and the original values will not be affected. in addition, we have other parameters passed to the method, including reference (REF) and output (out ).

Sometimes, we need to change the value of the original variable. In this case, we can pass the reference of the variable to the method, instead of the value of the variable. A reference is a variable that can access the value of the original variable. modifying a reference changes the value of the original variable. the value of a variable is stored in the memory. You can create a reference pointing to the position of the variable in the memory. when the reference is modified, the value in memory is modified, so the value of the variable can be modified. when we call a method that contains referenced parameters, the parameters in the method point to the corresponding variable passed to the method. Therefore, we will understand that, why does the modification of the Parameter Variable also lead to the value of the original variable.

First, let's talk about ref :( msdn)

C #ProgramMember reference

For ref, see

C # keywords | method parameters

The ref method parameter keyword in the method parameter allows the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable.

To use the ref parameter, you must explicitly pass the parameter as the ref parameter to the method. The ref parameter value is passed to the ref parameter.

Parameters passed to the ref parameter must be initialized first. Compared with the out parameter, the latter parameter does not need to be explicitly initialized before being passed to the out parameter.

Attributes are not variables and cannot be passed as REF parameters.

If the declarations of the two methods are only different in their use of ref, an overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload declaration is valid:

Class myclass

{

Public void mymethod (int I) {I = 10 ;}

Public void mymethod (ref int I) {I = 10 ;}

}

However, the following overload statement is invalid:

Class myclass

{

Public void mymethod (Out int I) {I = 10 ;}

Public void mymethod (ref int I) {I = 10 ;}

}

For information on passing arrays, see passing Arrays Using ref and out.

Example

// Cs_ref.cs

Using system;

Public class myclass

{

Public static void testref (ref char I)

{

// The value of I will be changed in the calling Method

I = B;

}

Public static void testnoref (char I)

{

// The value of I will be unchanged in the calling Method

I = C;

}

// This method passes a variable as a ref parameter; the value of

// Variable is changed after control passes back to this method.

// The Same variable is passed as a value parameter; the value of

// Variable is unchanged after control is passed back to this method.

Public static void main ()

{

Char I = A; // variable must be initialized

Testref (Ref I); // The ARG must be passed as REF

Console. writeline (I );

Testnoref (I );

Console. writeline (I );

}

}

Output

B

B

Here we will talk about the out:

C # programmer's reference

For more information, see

C # keywords | method parameters

The out method parameter keyword on the method parameter enables the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable.

When you want the method to return multiple values, it is very useful to declare the out method. You can still return a value using the out parameter. A method can have more than one out parameter.

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

You do not need to initialize the variable passed as the out parameter. However, the out parameter must be assigned a value before the method is returned.

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

If the declaration of the two methods is only different in the use of out, the overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload declaration is valid:

Class myclass

{

Public void mymethod (int I) {I = 10 ;}

Public void mymethod (Out int I) {I = 10 ;}

}

The following overload statement is invalid:

Class myclass

{

Public void mymethod (Out int I) {I = 10 ;}

Public void mymethod (ref int I) {I = 10 ;}

}

For information on passing arrays, see passing Arrays Using ref and out.

Example

// Cs_out.cs

Using system;

Public class myclass

{

Public static int testout (Out char I)

{

I = B;

Return-1;

}

Public static void main ()

{

Char I; // variable need not be initialized

Console. writeline (testout (Out I ));

Console. writeline (I );

}

}

Output

-1

B

I hope you can use these two keywords in C. It brings convenience to your programming. Thank you.

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.