RefKeywords are often used in C #, but they have never been carefully considered. I often write code, but I haven't figured out the basics yet. I used to think that using ref is a reference. If you change the parameter value in this method, the value of the variable corresponding to this parameter will also be changed.
Note: A method without ref is called only a copy of the parameter. When a ref is passed in, it is a reference of the call.
The following code is an example on msdn:
1 // cs_ref.cs
2 using system;
3 Public class myclass
4 {
5 public static void testref (ref char I)
6 {
7 // The value of I will be changed in the calling Method
8 I = 'B ';
9}
10
11 public static void testnoref (char I)
12 {
13 // The value of I will be unchanged in the calling Method
14 I = 'C ';
15}
16
17 // This method passes a variable as a ref parameter; the value of
18 // variable is changed after control passes back to this method.
19 // the same variable is passed as a value parameter; the value of
20 // variable is unchanged after control is passed back to this method.
21 public static void main ()
22 {
23
24 char I = 'a'; // variable must be initialized
25 testref (Ref I); // The ARG must be passed as REF
26 console. writeline (I );
27 testnoref (I );
28 console. writeline (I );
29}
30}
The output result is:
bb
In the preceding example, the value type is used as the parameter. What will happen if an object is used as a parameter? See the following code:
1 // cs_object_ref.cs
2 using system;
3
4 public class myobject
5 {
6 Public int data;
7}
8
9 public class myclass
10 {
11 public static void testref (ref myobject m)
12 {
13 // The value of myobject. Data will be changed in the calling Method
14 m. Data = 10;
15}
16
17 public static void testnoref (myobject m)
18 {
19 // The value of myobject. Data will be unchanged in the calling Method
20 M. Data = 20;
21}
22
23 public static void testcreateref (ref myobject m)
24 {
25 m = new myobject ();
26 M. Data = 100;
27}
28
29 public static void testcreatenoref (myobject m)
30 {
31 m = new myobject ();
32 M. Data = 200;
33}
34
35 public static void main ()
36 {
37
38 myobject M = new myobject ();
39 m. Data = 1;
40
41 testref (ref m );
42 console. writeline (M. data );
43
44 testnoref (m );
45 console. writeline (M. data );
46
47 testcreateref (ref m );
48 console. writeline (M. data );
49
50 testcreatenoref (m );
51 console. writeline (M. data );
52}
53}
Output result:
1020100100
We can see that the value of the parameter is modified in the testref and testnoref methods, and the value of the corresponding variable is changed, because they are passed references :) but in the following two methods: testcreateref and testcreatenoref, their results are not the same.
When you re-create an instance using the ref keyword, not only is the reference of the variable in the method updated (of course, what else do you do to create an instance :)), the reference of variables in the caller is also updated. It will be like using the new keyword to create an object within the caller's range.
In the method that does not use the ref keyword, the variable reference in this method is changed, and the reference of the caller variable is not changed. That is to say, the caller will not know any changes to the newly created variables in the testcreatenoref method!