C#结构体和类的区别浅析

开发 后端
C#结构体和类的区别是什么呢?C#结构体和类的区别是如何表现的呢?本文就向你讲述这方面的内容。

C#结构体和类的区别问题:在C#编程语言中,类属于引用类型的数据类型,结构体属于值类型的数据类型,这两种数据类型的本质区别主要是各自指向的内存位置不同。传递类的时候,主要表现为是否同时改变了源对象。

C#结构体和类的区别技术要点:

◆类在传递的时候,传递的内容是位于托管内存中的位置,结构体在传递的时候,传递的内容是位于程序堆栈区的内容。当类的传递对象修改时,将同时修改源对象,而结构体的传递对象修改时,不会对源对象产生影响。

◆在一个类中,可以定义默认的、不带参数的构造函数,而在结构体中不能定义默认的、不带参数的构造函数。两者都可以定义带有参数的构造函数,通过这些参数给各自的字段赋值或初始化。

C#结构体和类的区别之实现步骤

(1)创建控制台应用程序项目,命名为“ClassAndStruct”。

(2)打开并编辑Program.cs文件,代码如下所示。

  1. using System;   //C#结构体和类的区别
  2.  
  3. using System.Collections.Generic;   
  4.  
  5. using System.Text;   
  6.  
  7. namespace ClassAndStruct   
  8.  
  9. {   
  10.  
  11. class Program   
  12.  
  13. {   
  14.  
  15. static void Main(string[] args)   
  16.  
  17. {   
  18.  
  19. //使用带参数的构造函数创建员工类的实例   
  20.  
  21. classEmployee clsEmpA = new 
  22. classEmployee("Pony","Smith",43);   
  23.  
  24. classEmployee clsEmpB = clsEmpA;   
  25.  
  26. //修改引用数据   
  27.  
  28. clsEmpB.Age = 33;   
  29.  
  30. //使用带参数的构造函数创建员工结构体的实例   
  31.  
  32. structEmployee strctEmpA = 
  33. new structEmployee("Pony""Smith", 43);   
  34.  
  35. structEmployee strctEmpB = strctEmpA;   
  36.  
  37. //修改   
  38.  
  39. strctEmpB.Age = 33;   
  40.  
  41. Console.WriteLine("类的数据:姓名-{0} {1} 年龄-{2}",   
  42.  
  43. clsEmpA.FirstName,clsEmpA.LastName,clsEmpA.Age);   
  44.  
  45. Console.WriteLine("结构体的数据:姓名-{0} {1} 年龄-{2}",   
  46.  
  47. strctEmpA.FirstName, strctEmpA.LastName, strctEmpA.Age);   
  48.  
  49. Console.ReadLine();   
  50.  
  51. }   
  52.  
  53. }   
  54.  
  55. class classEmployee//表示员工的类   
  56.  
  57. {   
  58.  
  59. private string firstname;   
  60.  
  61. public string FirstName   
  62.  
  63. {   
  64.  
  65. get { return firstname; }   
  66.  
  67. set { firstname = value; }   
  68.  
  69. }   
  70.  
  71. private string lastname;   
  72.  
  73. public string LastName   
  74.  
  75. {   
  76.  
  77. get { return lastname; }   
  78.  
  79. set { lastname = value; }   
  80.  
  81. }   
  82.  
  83. private int age;   
  84.  
  85. public int Age   
  86.  
  87. {   
  88.  
  89. get { return age; }   
  90.  
  91. set { age = value; }   
  92.  
  93. }   
  94.  
  95. //类的默认构造函数,可以在类中重新定义   
  96.  
  97. public classEmployee()   
  98.  
  99. {   
  100.  
  101. firstname = "";   
  102.  
  103. lastname = "";   
  104.  
  105. age = 0;   
  106.  
  107. }   
  108.  //C#结构体和类的区别
  109. //类的带参数的构造函数,在构造类实例的同时给字段赋值   
  110.  
  111. public classEmployee(
  112. string strFirstNamem, string strLastName, int iAge)   
  113.  
  114. {   
  115.  
  116. firstname = strFirstNamem;   
  117.  
  118. lastname = strLastName;   
  119.  
  120. age = iAge;   
  121.  
  122. }   
  123.  
  124. }   
  125.  
  126. struct structEmployee//表示员工的结构体   
  127.  
  128. {   
  129.  
  130. private string firstname;   
  131.  
  132. public string FirstName   
  133.  
  134. {   
  135.  
  136. get { return firstname; }   
  137.  
  138. set { firstname = value; }   
  139.  
  140. }   
  141.  
  142. private string lastname;   
  143.  
  144. public string LastName   
  145.  
  146. {   
  147.  
  148. get { return lastname; }   
  149.  
  150. set { lastname = value; }   
  151.  
  152. }   
  153.  //C#结构体和类的区别
  154. private int age;   
  155.  
  156. public int Age   
  157.  
  158. {   
  159.  
  160. get { return age; }   
  161.  
  162. set { age = value; }   
  163.  
  164. }   
  165.  
  166. //在结构体中不能定义默认的不带参数的构造函数,只能定义结构体的带参数的构造函数   
  167.  
  168. public structEmployee(string strFirstNamem, 
  169. string strLastName, int iAge)   
  170.  
  171. {   
  172.  
  173. firstname = strFirstNamem;   
  174.  
  175. lastname = strLastName;   
  176.  
  177. age = iAge;   
  178.  
  179. }   
  180.  
  181. }   
  182.  
  183. }  

(3)按F5键运行程序,运行结果如下所示。

类的数据:姓名-Pony Smith 年龄-33

结构体的数据:姓名-Pony Smith 年龄-43

C#结构体和类的区别之源程序解读

(1)本示例为了说明在传递时C#结构体和类的区别,在程序中分别定义了表示员工的类classEmployee类和表示员工的结构体structEmployee,并定义了各自的字段和构造函数。在主程序入口Main方法中,声明类的实例clsEmpA和clsEmpB,并使用构造函数创建clsEmpA类实例,然后将clsEmpA类实例传递给clsEmpB类实例,修改clsEmpB类实例的字段值,最后打印clsEmpA类实例中的字段,查看字段的值是否随clsEmpB类实例字段的修改而变化。同时,声明结构体的实例strctEmpA和strctEmpB,并使用构造函数创建strctEmpA结构体实例,然后将strctEmpA结构体实例传递给strctEmpB结构体实例,修改strctEmpB结构体实例的字段值,最后打印strctEmpA结构体实例中的字段,查看字段的值是否随strctEmpB结构体实例字段的修改而变化。程序的流程图如图8.1所示。 

(2)C#结构体和类的区别还有一个比较明显的区别,就是类能够定义默认的、不带参数的构造函数,并能在该构造函数中初始化字段。而结构体不允许定义默认的、不带参数的构造函数。

C#结构体和类的区别的相关内容就向你介绍到这里,希望对你学习和了解C#结构体和类的区别有所帮助。

【编辑推荐】

  1. C#结构体的特点浅析
  2. C#结构体数组间的转化浅析
  3. 解决C#结构体数组间的转化
  4. C#结构体使用浅析
  5. C#构造函数介绍及分类浅析
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-27 16:18:47

C#类C#结构体

2009-08-13 11:18:50

C#结构体

2009-08-13 13:29:04

C#结构体使用

2009-08-13 14:56:46

C#的结构体使用

2009-08-31 15:02:22

C#解析结构体指针

2009-08-13 13:03:52

C#结构体数组

2009-08-17 18:04:49

C# 枚举

2009-08-21 17:24:06

C# SingleIn

2009-08-21 17:24:06

C# SingleIn

2009-08-06 14:43:10

C# Calculat

2009-08-21 11:31:59

异步和多线程的区别

2009-08-14 11:05:28

C#语言的结构体

2009-08-13 14:46:03

C#结构体定义

2009-08-27 13:37:11

C#类和结构

2009-09-23 09:36:34

C#数组

2009-08-10 10:37:17

C#类与结构

2009-08-13 14:24:44

C#结构体构造函数

2010-07-12 09:07:30

C#

2009-08-13 17:30:30

C#构造函数

2009-08-13 15:03:58

C#结构体变量
点赞
收藏

51CTO技术栈公众号