The difference between ref and out in C #

Source: Internet
Author: User

Reprint Original Address http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html

The difference between ref and out is that in C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword. The two keywords, ref and out, can provide similar effects and act like pointer variables in C. The difference between them is:

1. When using a ref parameter, the passed-in parameter must first be initialized. For out, initialization must be done in the method.

2. When using ref and out, the ref or out keyword is added to the method's parameters and execution methods. To meet the match.

3. Out is suitable for use where multiple return values need to be retrun, while ref is used to modify the caller's reference in a method that needs to be called.

Note: In C #, there are four types of method parameter passing:

Pass value (by value)

Address (by reference)

Output parameters (by output)

Array parameters (by array)

The pass-through parameter requires no additional modifiers, the address parameter needs a modifier ref, the output parameter requires a modifier out, and the array parameter requires a modifier params. If the value of the parameter is changed during a method call, the parameters of the passed-in method do not change after the method call is complete, but instead retain the value of the original pass-in. On the contrary, if the method invocation procedure changes the value of the parameter, the parameters of the incoming method are changed after the call is completed. In fact, from the name we can clearly see the meaning of the two--the pass parameter is passed a copy of the invocation parameter, and the address parameter is passed the memory of the calling parameter, which is the same storage location inside and outside the method.

The ref method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.

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

The arguments passed to the ref parameter must be initialized first. Comparing this method to an out parameter, the latter's arguments do not have to be explicitly initialized before they are passed to the out parameter.

property is not a variable and cannot be passed as a ref parameter.

If the declarations of both methods differ only in their use of ref, an overload occurs. However, you cannot define overloads that differ only in respect of ref and out.

Out

The Out method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.

Declaring an out method is useful when you want a method to return more than one value. Methods that use out parameters can still return a value. A method can have more than one out parameter.

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

You do not have to initialize a variable passed as an out parameter. However, you must assign a value to the out parameter before the method returns.

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


There are many articles on the internet saying that ref is only a value, out of the address, and so on, it seems not very accurate. Here is the example code I made, you can try it out:

  Public intRefvalue (intIref intj) {intK =J; J=222; returni+K; }               Public intOutvalue (intI out intj) {J=222; returni +J; }        Private voidCmdref_click (Objectsender, EventArgs e) {            intm =0; MessageBox.Show (Refvalue (1,refm).            ToString ());        MessageBox.Show (M.tostring ()); }        Private voidCmdout_click (Objectsender, EventArgs e) {            intm; MessageBox.Show (Outvalue (1, outm).            ToString ());        MessageBox.Show (M.tostring ()); }

In a short sentence on the net,ref is there and out, and out is just out of the

The difference between ref and out in C #

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.