classPassingRefByVal{staticvoidChange(int[] pArray){
pArray[0]=888;// This change affects the original element.
pArray =newint[5]{-3,-1,-2,-3,-4};// This change is local.
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);}staticvoidMain(){int[] arr ={1,4,5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);Change(arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);}}/* Output:
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888
*/
引用传递
classPassingRefByRef{staticvoidChange(refint[] pArray){// Both of the following changes will affect the original variables:
pArray[0]=888;
pArray =newint[5]{-3,-1,-2,-3,-4};
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);}staticvoidMain(){int[] arr ={1,4,5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);Change(ref arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);}}/* Output:
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3
*/