值类型在赋值的时候,传递的是这个值的本身。
    引用类型在赋值的时候,传递的是对这个对象的引用(地址)。
    https://www.bilibili.com/video/BV1FJ411W7e5?p=148&spm_id_from=pageDriver
    image.png
    image.png
    //ref 使实参和形参的地址相同 (值传递->引用传递(不占用堆地址),应该不完全对)

    1. using System;
    2. namespace _087_值传递和引用传递
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. //值类型:int double char decimal bool enum struct
    9. //引用类型:string 数组 自定义类 集合 object 接口
    10. //值传递和引用传递
    11. int n1 = 1;
    12. int n2 = n1;
    13. Console.WriteLine(n1);
    14. Console.WriteLine(n2);
    15. Console.WriteLine("==============================");
    16. Person p1 = new Person();
    17. p1.Name = "张三";
    18. Person p2 = p1;// 堆里,同一地址
    19. p2.Name = "李四";
    20. p1.Name = "abc";
    21. Person p3 = new Person();
    22. p3.Name = "王五";
    23. Console.WriteLine(p1.Name);
    24. Console.WriteLine(p2.Name);
    25. Console.WriteLine(p3.Name);
    26. Console.WriteLine("==============================");
    27. Person p = new Person();
    28. p.Name = "张三";
    29. Test(p);
    30. Console.WriteLine(p.Name);
    31. Console.WriteLine("==============================");
    32. //string的不可变性 每次赋值重新开辟空间
    33. string s1 = "张三";
    34. string s2 = s1;
    35. s2 = "李四";
    36. Console.WriteLine(s1);
    37. Console.WriteLine(s2);
    38. Console.WriteLine("==============================");
    39. //ref 使实参和形参的地址相同 (值传递->引用传递,应该不完全对)
    40. int number = 10;
    41. TestTwo(ref number);
    42. Console.WriteLine(number);
    43. Console.ReadKey();
    44. }
    45. /// <summary>
    46. /// 给Person类实例中的Name属性赋值
    47. /// </summary>
    48. /// <param name="pp"></param>
    49. public static void Test(Person pp)
    50. {
    51. Person ppp = pp;
    52. ppp.Name = "李四";
    53. //Person p = pp;
    54. //p.Name = "李四";
    55. }
    56. /// <summary>
    57. /// 给n加10
    58. /// </summary>
    59. /// <param name="n"></param>
    60. public static void TestTwo(ref int n)
    61. {
    62. n += 10;
    63. }
    64. }
    65. }
    66. using System;
    67. using System.Collections.Generic;
    68. using System.Text;
    69. namespace _087_值传递和引用传递
    70. {
    71. class Person
    72. {
    73. private string _name;
    74. public string Name { get => _name; set => _name = value; }
    75. }
    76. }