C#:浅析结构与类的区别

一、
     结构:值类型,存储在堆栈中,位于计算机的内存逻辑区域中
     类   :引用类型,存储在堆中,位于计算机内存的不同逻辑位置
 
二、
     较小的数据使用结构;
     将一个结构值传递到方法时,传递的是整个数据结构;
     传递一个类,实际上是将引用传递到对象,即只有内存地址;
     对结构修改,改变的是结构的副本,这是值类型工作方式的定义:传递值的副本;
     传递一个引用到类本身意味着在类中修改值,实际上改变的是原始对象;
 
三、代码栗子
  1.新建 PointClass.cs
复制代码
 1 namespace StructAndClass
 2 {
 3     internal class PointClass
 4     {
 5         public PointClass(int x, int y)
 6         {
 7             X = x;
 8             Y = y;
 9         }
10 
11         public int X { get; set; }
12 
13         public int Y { get; set; }
14     }
15 }
复制代码

 

  2.新建 PointStruct.cs

复制代码
 1 namespace StructAndClass
 2 {
 3     internal struct PointStruct
 4     {
 5         public int X { get; set; }
 6 
 7         public int Y { get; set; }
 8 
 9         public PointStruct(int x, int y)
10         {
11             X = x;
12             Y = y;
13         }
14     }
15 }
复制代码

 

  3.Program.cs

复制代码
 1 using System;
 2 
 3 namespace StructAndClass
 4 {
 5     internal class Program
 6     {
 7         private static void Main(string[] args)
 8         {
 9             Console.WriteLine("PointStruct =====");
10             var pStruct = new PointStruct(10, 10);
11             Console.WriteLine("初始值:x={0},y={1}", pStruct.X, pStruct.Y);
12             ModifyPointStruct(pStruct);
13             Console.WriteLine("调用 ModifyPointStruct() 后的值:x={0},y={1}", pStruct.X, pStruct.Y);
14             Console.WriteLine();
15 
16             Console.WriteLine("PointClass =====");
17             var pClass = new PointClass(10, 10);
18             Console.WriteLine("初始值:x={0},y={1}", pClass.X, pClass.Y);
19             ModifyPointClass(pClass);
20             Console.WriteLine("调用 ModifyPointClass() 后的值:x={0},y={1}", pClass.X, pClass.Y);
21             Console.Read();
22         }
23 
24         private static void ModifyPointStruct(PointStruct point)
25         {
26             Console.WriteLine("调用方法:ModifyPointStruct");
27             point.X = 20;
28             point.Y = 20;
29             Console.WriteLine("修改成的值:x={0}, y={1}", point.X, point.Y);
30         }
31 
32         private static void ModifyPointClass(PointClass point)
33         {
34             Console.WriteLine("调用方法:ModifyPointClass");
35             point.X = 20;
36             point.Y = 20;
37             Console.WriteLine("修改成的值:x={0}, y={1}", point.X, point.Y);
38         }
39     }
40 }
复制代码

 

  4.结果:

【解析】

  ModifyPointStruct(PointStruct point) 调用时修改的只是结构副本,所以原来的结构并没有发生变化;  
  ModifyPointClass(PointClass point) 调用时所修改的对象是原对象,因为参数传递过来的是一个引用地址,这地址指向原对象

四、总结

  结构是值类型并在堆栈中传递,每次使用方法进行修改的都只是结构副本;

      至于类,传递的是内存地址的引用,修改的就是初始值

 

posted @   反骨仔  阅读(2996)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示