• C# 值类型与引用类型


    值类型:
    int ,short ,byte ,char ,bool ,double ,float ,struct ,enum ,decimal
    储存在栈里面,效率高,用完直接释放,但无法存储大小不固定
    值类型进行赋值的时候是把数据拷贝了一个副本进行赋值,因为数据直接存储在
    栈中,所以把栈拷贝了一份即把数据拷贝了一份。

    引用类型:
    string ,数组 ,类 ,接口 ,委托
    栈里面存的是对象的地址(引用),内容存储在堆里面
    储存在堆里面,效率低,需要垃圾回收,可以储存大小不固定
    引用类型赋值,是把栈中对象的地址拷贝了一份,因为栈中是存储的对象的地址
    而对象的数据都存储在了堆中,所以引用类型赋值,只是把对象的地址赋值给了另一个变量

    值传递,传递的是栈中的内容,(对于值类型,栈中的内容就是对应的数据。对于引用类型栈中内容就是对象的地址)
    引用传递,传递的是栈本身的地址,多个变量名实际上指向的是同一个栈变量。
    引用传递必须使用ref关键字修饰。在方法调用的时候传递参数的时候也必须加ref 关键字

    值类型 与 引用类型 作为参数(值传递)

     1     class Program
     2     {
     3         #region 值类型 与 引用类型 作为参数 【值传递】
     4         static void Main(string[] args)
     5         {
     6             //int n = 10;
     7             //M1(n);
     8             //Console.WriteLine(n);//10
     9             //Console.ReadKey();
    10 
    11             //Person p = new Person();
    12             //p.Age = 100;
    13             //M2(p);
    14             //Console.WriteLine(p.Age);//101
    15 
    16             //Person p = new Person();
    17             //p.Age = 100;
    18             //M3(p);
    19             //Console.WriteLine(p.Age);//1000
    20 
    21             //Person p = new Person();
    22             //p.Age = 100;
    23             //M4(p);
    24             //Console.WriteLine(p.Age);//100
    25 
    26             //string name = "123";
    27             //M5(name);
    28             //Console.WriteLine(name);//"123"
    29 
    30             //int[] arrInt = new int[] { 1, 2, 3, 4, 5 };
    31             //M6(arrInt);
    32             //for (int i = 0; i < arrInt.Length; i++)
    33             //{
    34             //    Console.WriteLine(arrInt[i]);//1,2,3,4,5
    35             //}
    36             //Console.ReadKey();
    37 
    38             //int[] arrInt = new int[] { 1, 2, 3, 4, 5 };
    39             //M7(arrInt);
    40             //for (int i = 0; i < arrInt.Length; i++)
    41             //{
    42             //    Console.WriteLine(arrInt[i]);//100,100,100,100,100
    43             //}
    44             //Console.ReadKey();
    45         }
    46 
    47         private static void M7(int[] arrInt)
    48         {
    49             for (int i = 0; i < arrInt.Length; i++)
    50             {
    51                 arrInt[i] = 100;
    52             }
    53         }
    54 
    55         private static void M6(int[] arrInt)
    56         {
    57             arrInt = new int[arrInt.Length];
    58             for (int i = 0; i < arrInt.Length; i++)
    59             {
    60                 arrInt[i] = arrInt[i] * 2;
    61             }
    62         }
    63 
    64         private static void M5(string name)
    65         {
    66             name = "456";
    67         }
    68 
    69         private static void M4(Person p)
    70         {
    71             p = new Person();
    72             p.Age++;
    73         }
    74 
    75         private static void M3(Person p)
    76         {
    77             p.Age = 1000;
    78             p = new Person();
    79             p.Age = 200;
    80         }
    81 
    82         private static void M2(Person p)
    83         {
    84             p.Age++;
    85         }
    86 
    87         private static void M1(int n)
    88         {
    89             n++;
    90         }
    91 
    92         class Person
    93         {
    94             public int Age { get; set; }
    95         }
    96 
    97         #endregion
    98     }
    View Code

    值类型 与 引用类型 作为参数(引用传递)

      1     class Program
      2     {
      3         static void Main(string[] args)
      4         {
      5 
      6             #region 值类型 与 引用类型 作为参数  【引用传递】
      7             //int n = 10;
      8             //M1(ref n);
      9             //Console.WriteLine(n);//11
     10             //Console.ReadKey();
     11 
     12             //Person p = new Person();
     13             //p.Age = 100;
     14 
     15             //M2(ref p);
     16 
     17             //Console.WriteLine(p.Age);//101
     18 
     19             //Person p = new Person();
     20             //p.Age = 100;
     21             //M3(ref p);
     22             //Console.WriteLine(p.Age);//
     23 
     24             //Person p = new Person();
     25             //p.Age = 100;
     26             //M4(ref p);
     27             //Console.WriteLine(p.Age);//1
     28 
     29             //string name = "科比";
     30             //M5(ref name);
     31             //Console.WriteLine(name);//乔丹
     32 
     33             //int[] arrInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
     34             //M6(ref arrInt);
     35             ////0000000
     36             //for (int i = 0; i < arrInt.Length; i++)
     37             //{
     38             //    Console.WriteLine(arrInt[i]);
     39             //}
     40 
     41             int[] arrInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
     42             M7(ref arrInt);
     43             //??????????
     44             for (int i = 0; i < arrInt.Length; i++)
     45             {
     46                 Console.WriteLine(arrInt[i]);
     47             }
     48 
     49             
     50             Console.ReadKey();
     51             #endregion
     52         }
     53 
     54         #region 
     55         private static void M7(ref int[] arrInt)
     56         {
     57             for (int i = 0; i < arrInt.Length; i++)
     58             {
     59                 arrInt[i] = 100;
     60             }
     61         }
     62 
     63         private static void M6(ref int[] arrInt)
     64         {
     65             arrInt = new int[arrInt.Length];
     66             for (int i = 0; i < arrInt.Length; i++)
     67             {
     68                 arrInt[i] = arrInt[i] * 2;
     69             }
     70         }
     71 
     72 
     73         private static void M5(ref string name2)
     74         {
     75             name2 = "乔丹";
     76         }
     77 
     78 
     79 
     80         private static void M4(ref Person p1)
     81         {
     82             p1 = new Person();
     83             p1.Age++;
     84         }
     85 
     86 
     87         private static void M3(ref Person p1)
     88         {
     89             p1.Age = 1000;
     90             p1 = new Person();
     91             p1.Age = 200;
     92         }
     93 
     94 
     95 
     96 
     97         private static void M2(ref Person p1)
     98         {
     99             p1.Age++;
    100         }
    101 
    102 
    103         private static void M1(ref int m)
    104         {
    105             m++;
    106         }
    107 
    108         #endregion
    109     }
    View Code
  • 相关阅读:
    大物—⑦热力学基础1
    组原——③系统总线
    组原——①计算机系统概论
    递归
    字符串
    Linux下安装glibc-2.14,解决“`GLIBC_2.14' not found”问题
    libc.so.6被删除修复ln: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
    正则表达式高级用法(分组与捕获)
    查看Android应用包名、Activity的几个方法
    logcat随笔
  • 原文地址:https://www.cnblogs.com/ggsdduzbl/p/5049922.html
Copyright © 2020-2023  润新知