传引用:
传引用可以改变变量的值,传值只能改变变量在方法中的拷贝;
代码:
static void Main() { int x = 1, y = 10; swap(ref x, ref y); //改变了x,y的值,不写ref编译器会报错 Console.WriteLine("{0}{1}",x,y); Console.ReadLine(); } static void swap(ref int a, ref int b) { int t = a; a = b; b = t; }
参数数组:
使用关键字params定义数量可变的参数, 调用方法时这些参数可以用逗号分隔,也可以用一个数组传递
注意的是:
1.参数数组必须是方法声明中的最后一个参数
2.调用时可以指定零个参数
3.使用逗号分隔的参数列表和使用一个数组参数,生成的CIL代码是一样的
4.要声明一个最起码的参数,要写在参数数组前面。
代码:
static void Main() { string[] s = { "aaa", "bbb" }; output(s); output(); output("abc", "cde", "efg"); Console.ReadLine(); } static void output(params string[]args) { foreach (string str in args) { Console.WriteLine(str); } }