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

时间:2023-03-09 09:28:03
C#基础:值类型、引用类型与ref关键字

在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. static void Main(string[] args)
  8. {
  9. int b = 1;
  10. Test(ref b);
  11. Console.WriteLine(b);
  12. }
  13. }

此时的输出是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. static void Main(string[] args)
  8. {
  9. int b = 1;
  10. Test(b);
  11. Console.WriteLine(b);
  12. }
  13. }

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

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

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

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