Differences Between out and ref in C #

Source: Internet
Author: User

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.

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.

Both are transmitted by address, and the original values are changed after use.
Ref can pass the value of the parameter to the function, but the out parameter must be cleared.
That is to say, you cannot pass a value from out. After an out value is input, the parameter value is null, so you must initialize it once.
There are two differences: ref indicates inbound and outbound, while out indicates outbound.

The code example is as follows:

1 namespace testoutandref
2 {
3 class testapp
4 {
5
6 static void outtest (Out int X, out int y)
7 {// before leaving this function, you must assign values to X and Y; otherwise, an error is returned.
8 // y = X;
9 // The above line returns an error, because after the out is used, both X and Y are cleared, and the value must be assigned again, even if the value is assigned before the function is called.
10 x = 1;
11 y = 2;
12}
13 static void reftest (ref int X, ref int y)
14 {
15 x = 1;
16 Y = X;
17}
18
19
20 static public void outarray (Out int [] myarray)
21 {
22 // initialize the array:
23 myarray = new int [5] {1, 2, 3, 4, 5 };
24}
25 public static void fillarray (ref int [] ARR)
26 {
27 // create the array on demand:
28 If (ARR = NULL)
29 arr = new int [10];
30 // otherwise fill the array:
31 arr [0] = 123;
32 arr [4] = 1024;
33}
34
35
36 public static void main ()
37 {
38 // out Test
39 int A, B;
40 // before use, the variable can be not assigned a value
41 outtest (out a, out B );
42 console. writeline ("A = {0}; B = {1}", a, B );
43 int c = 11, D = 22;
44 outtest (out C, out d );
45 console. writeline ("c = {0}; D = {1}", C, D );
46
47 // ref Test
48 int M, N;
49 // reftest (ref M, ref N );
50 // the above Code goes wrong. Before using ref, the variable must be assigned a value.
51
52 int o = 11, P = 22;
53 reftest (ref o, ref P );
54 console. writeline ("O = {0}; P = {1}", O, P );
55
56
57
58 int [] myarray1; // Initialization is not required
59
60 // pass the array to the callee using out:
61 outarray (Out myarray1 );
62
63 // display the array elements:
64 console. writeline ("array1 elements are :");
65 for (INT I = 0; I <myarray1.length; I ++)
66 console. writeline (myarray1 [I]);
67
68 // initialize the array:
69 int [] myarray = {1, 2, 3, 4, 5 };
70
71 // pass the Array Using Ref:
72 fillarray (ref myarray );
73
74 // display the updated array:
75 console. writeline ("array elements are :");
76 for (INT I = 0; I <myarray. length; I ++)
77 console. writeline (myarray [I]);
78}
79}
80
81}

 

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.