- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
- 纳金币
- 20645
- 精华
- 62
|
- class Program
- {
- //ref 和out类似c++指针,当然也不能拿来直接当指针用,是能引用一些参数,但是不能直接作为地址来控制参数(大概意思)
- static void Main(string[] args)
- {
- int a, b;
- OutTest(out a, out b);
- Console.WriteLine("a={0},b={1}",a,b);
- //output a = 1;b = 2;
- int c = 11, d = 22;
- OutTest(out c, out d);
- Console.WriteLine("a={0},b={1}", a, b);
- //output c = 1;d = 2;
- int m = 11, n = 22;
- //RefTest(ref m, ref n);
- RefTest(ref m, ref n);
- Console.WriteLine("m={0},n={1}", m, n);
- //output m = 22;n = 11;
- Swap(m, n);
- Console.WriteLine("m={0},n={1}", m, n);
- //output m = 22;n = 11;
- }
- static void OutTest(out int x, out int y)
- {
- //方法体外可以不用对传出参数赋值
- //方法体内,必须对传出的参数赋值,否则报错,例如:x=y;
- //适用于多个返回值的函数
- x = 1;
- y = 2;
- }
- static void RefTest(ref int x, ref int y)
- {
- //方法体外,必须对参数赋值,否则报错
- //方法体内,ref对参数可以赋值也可以不赋值
- //适合引用原值,并对原值修改
- int temp = x;
- x = y;
- y = temp;
- }
- static void Swap(int a, int b)
- {
- int temp = a;
- a = b;
- b = temp;
- }
- }
复制代码 |
|