C#函数的参数传递方式1(值传递与地址传递)
using System; namespace class1 { class program { static void Main(string[] args) { //值传递引用,实际参数不会变化 Console.Write("请输入a="); double a = double.Parse(Console.ReadLine()); Console.Write("请输入b="); double b = double.Parse(Console.ReadLine()); double c = max(a, b);//局部实际参数 Console.WriteLine("您输入的值中比较大的是{0}", c); //地址传递引用,实际参数会变化 int[] ex = new int[4] { 1, 2, 3, 4 }; Console.WriteLine("Before"); foreach (var item in ex) { Console.Write("\t{0}", item); } dou(ex); Console.WriteLine("\nEnd"); foreach (var item in ex) { Console.Write("\t{0}", item); } } /// <summary> /// 值传递 /// </summary> /// <param name="a">形式参数(参数为值类型)</param> /// <param name="b">形式参数(参数为值类型)</param> /// <returns></returns> static double max(double a, double b) {//局部形式参数 double c = a > b ? a : b; return c; } /// <summary> /// 地址传递 /// </summary> /// <param name="array">数组参数(参数为引用类型)</param> static void dou(int[] array) { for (int i = 0; i < array.Length; i++) { array[i] = array[i] * 2; } } } }