C#函数的参数传递方式1(值传递与地址传递)
程序员文章站
2023-11-14 09:58:16
using System; namespace class1 { class program { static void Main(string[] args) { //值传递引用,实际参数不会变化 Console.Write("请输入a="); double a = double.Parse(Co ......
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; } } } }