zoukankan      html  css  js  c++  java
  • 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.   
    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关键字。

  • 相关阅读:
    anconda + python 3.6安装(以前的anconda,anaconda和python版本对应关系)
    数学建模python matlab 编程(喷泉模拟)
    使用git checkout 指定git代码库上的指定分支
    aapt命令获取apk具体信息(包名、版本号号、版本号名称、兼容api级别、启动Activity等)
    toad for oracle中文显示乱码
    storm是怎样保证at least once语义的
    记使用WaitGroup时的一个错误
    Drupal 初次使用感受,兴许补充。
    Qt 5.5.0 Windows环境搭建
    有趣的Ruby-学习笔记3
  • 原文地址:https://www.cnblogs.com/yangwujun/p/4660274.html
Copyright © 2011-2022 走看看