First look at the Code:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace testRef {
Class testRef {
Private static void ChgArrVal (int [] array ){
For (int I = 0; I <array. Length; I ++)
Array [I] = (int) Math. Pow (array [I], 2 );
}
Private static void ChgVal (int I ){
I = (int) Math. Pow (2, I );
}
Private static void ChgValRef (ref int I ){
I = (int) Math. Pow (2, I );
}
Static void Main (string [] args ){
Int [] arr = new int [] {3, 4, 5, 6, 7, 8 };
Int val = 5;
Console. WriteLine ("original:" + val );
ChgVal (val );
Console. WriteLine ("ChgVal:" + val );
Console. WriteLine ("#####");
Console. WriteLine ("original:" + val );
ChgValRef (ref val );
Console. WriteLine ("ChgValRef:" + val );
Console. WriteLine ("#####");
Foreach (int a in arr)
Console. WriteLine (">>>" + );
ChgArrVal (arr );
Console. WriteLine ("#####");
Foreach (int B in arr)
Console. WriteLine (">>>" + B );
}
}
}
------------------- The following is the running result:
Output:
Original: 5
ChgVal: 5
#####
Original: 5
ChgValRef: 32
#####
>>> 3
>>> 4
>>> 5
>>> 6
>>> 7
>>> 8
#####
>>>> 9
>>>> 16
>>>> 25
>>>> 36
>>>> 49
>>>> 64
Why...
###########################
Did you see it? The original Ref intent is
The ref keyword causes arguments to be passed by reference. the parameter reference is passed, that is, the reference is passed and executed. The original variable value can be changed without the return value. but you can view the array without using Ref. it can be seen that when an array is used, it is just an address reference, and it is not an actual operation. Why not use Ref to change it?
I don't know, right? I think so.