C# 值类型与引用类型

本文详细解析了值类型和引用类型的存储位置、赋值方式及其作为参数时的区别,包括值传递与引用传递的不同表现,并通过示例代码进行了具体说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

值类型:
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

 

转载于:https://www.cnblogs.com/ggsdduzbl/p/5049922.html

内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值