现在的位置: 首页 > 综合 > 正文

C#基础:值类型、引用类型与ref关键字

2012年08月13日 ⁄ 综合 ⁄ 共 1056字 ⁄ 字号 评论关闭

在C#中,ref的意思是按引用传递。可以参考C++:

1 int a = 10, b = 20;    
2 void swap(int x, int y)
3 {
4 int temp = x;
5 x = y;
6 y = temp;
7 }

如果简单的调用这个swap,比如:swap(a, b),那么你根本没办法交换这两个变量的值,因为x和y都是形参,在swap返回的时候,x和y都被释放了。但如果是这样定义swap:

1 void swap (int& x, int& y)    
2 {
3 int temp = x;
4 x = y;
5 y = temp;
6 }

也就相当于x与a,y与b指向同一个内存地址,那么对x的操作也就相当于对a的操作。那么在C#里面,这种效果对于值类型是很明显的。

 1 class Program    
2 {
3 static void Test(ref int b)
4 {
5 b = 2;
6 }
7
8 static void Main(string[] args)
9 {
10 int b = 1;
11 Test(ref b);
12 Console.WriteLine(b);
13 }
14 }

此时的输出是2,也就是Test方法中的b与Main中的b指向同一个内存地址,那么对Test.b的操作也就是对Main.b的操作。你如果把程序改成:

 1 class Program    
2 {
3 static void Test(int b)
4 {
5 b = 2;
6 }
7
8 static void Main(string[] args)
9 {
10 int b = 1;
11 Test(b);
12 Console.WriteLine(b);
13 }
14 }

那么输出的还是1,因为Test.b不是Main.b的引用,也就是一个单独的形参。现在再看对于引用类型,会是什么效果:

 1 class TestClass    
2 {
3 public int b;
4 }
5
6 class Program
7 {
8 static void Test(TestClass b)
9 {
10 b.b = 2;
11 }
12
13 static void Main(string[] args)
14 {
15 TestClass b = new TestClass();
16 b.b = 1;
17 Test(b);
18 Console.WriteLine(b.b);
19 }
20 }

上面的代码,输出的是2,因为b是引用类型,在只需修改b的成员的时候,加不加ref关键字都一样。引用类型本身并不包含数据,仅仅维持了对数据的引用。

因此,使用ref参数,对值类型对象的作用显而易见,而对于引用类型,如需修改引用类型内部的数据,则无需使用ref关键字;否则,当被调用函数内部需要更改引用本身时,比如在函数内部重新定位对象的引用,则需要使用ref关键字。

抱歉!评论已关闭.