自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#結(jié)構(gòu)體和類的區(qū)別淺析

開(kāi)發(fā) 后端
C#結(jié)構(gòu)體和類的區(qū)別是什么呢?C#結(jié)構(gòu)體和類的區(qū)別是如何表現(xiàn)的呢?本文就向你講述這方面的內(nèi)容。

C#結(jié)構(gòu)體和類的區(qū)別問(wèn)題:在C#編程語(yǔ)言中,類屬于引用類型的數(shù)據(jù)類型,結(jié)構(gòu)體屬于值類型的數(shù)據(jù)類型,這兩種數(shù)據(jù)類型的本質(zhì)區(qū)別主要是各自指向的內(nèi)存位置不同。傳遞類的時(shí)候,主要表現(xiàn)為是否同時(shí)改變了源對(duì)象。

C#結(jié)構(gòu)體和類的區(qū)別技術(shù)要點(diǎn):

◆類在傳遞的時(shí)候,傳遞的內(nèi)容是位于托管內(nèi)存中的位置,結(jié)構(gòu)體在傳遞的時(shí)候,傳遞的內(nèi)容是位于程序堆棧區(qū)的內(nèi)容。當(dāng)類的傳遞對(duì)象修改時(shí),將同時(shí)修改源對(duì)象,而結(jié)構(gòu)體的傳遞對(duì)象修改時(shí),不會(huì)對(duì)源對(duì)象產(chǎn)生影響。

◆在一個(gè)類中,可以定義默認(rèn)的、不帶參數(shù)的構(gòu)造函數(shù),而在結(jié)構(gòu)體中不能定義默認(rèn)的、不帶參數(shù)的構(gòu)造函數(shù)。兩者都可以定義帶有參數(shù)的構(gòu)造函數(shù),通過(guò)這些參數(shù)給各自的字段賦值或初始化。

C#結(jié)構(gòu)體和類的區(qū)別之實(shí)現(xiàn)步驟

(1)創(chuàng)建控制臺(tái)應(yīng)用程序項(xiàng)目,命名為“ClassAndStruct”。

(2)打開(kāi)并編輯Program.cs文件,代碼如下所示。

  1. using System;   //C#結(jié)構(gòu)體和類的區(qū)別
  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. //使用帶參數(shù)的構(gòu)造函數(shù)創(chuàng)建員工類的實(shí)例   
  20.  
  21. classEmployee clsEmpA = new 
  22. classEmployee("Pony","Smith",43);   
  23.  
  24. classEmployee clsEmpB = clsEmpA;   
  25.  
  26. //修改引用數(shù)據(jù)   
  27.  
  28. clsEmpB.Age = 33;   
  29.  
  30. //使用帶參數(shù)的構(gòu)造函數(shù)創(chuàng)建員工結(jié)構(gòu)體的實(shí)例   
  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("類的數(shù)據(jù):姓名-{0} {1} 年齡-{2}",   
  42.  
  43. clsEmpA.FirstName,clsEmpA.LastName,clsEmpA.Age);   
  44.  
  45. Console.WriteLine("結(jié)構(gòu)體的數(shù)據(jù):姓名-{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. //類的默認(rèn)構(gòu)造函數(shù),可以在類中重新定義   
  96.  
  97. public classEmployee()   
  98.  
  99. {   
  100.  
  101. firstname = "";   
  102.  
  103. lastname = "";   
  104.  
  105. age = 0;   
  106.  
  107. }   
  108.  //C#結(jié)構(gòu)體和類的區(qū)別
  109. //類的帶參數(shù)的構(gòu)造函數(shù),在構(gòu)造類實(shí)例的同時(shí)給字段賦值   
  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//表示員工的結(jié)構(gòu)體   
  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#結(jié)構(gòu)體和類的區(qū)別
  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. //在結(jié)構(gòu)體中不能定義默認(rèn)的不帶參數(shù)的構(gòu)造函數(shù),只能定義結(jié)構(gòu)體的帶參數(shù)的構(gòu)造函數(shù)   
  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鍵運(yùn)行程序,運(yùn)行結(jié)果如下所示。

類的數(shù)據(jù):姓名-Pony Smith 年齡-33

結(jié)構(gòu)體的數(shù)據(jù):姓名-Pony Smith 年齡-43

C#結(jié)構(gòu)體和類的區(qū)別之源程序解讀

(1)本示例為了說(shuō)明在傳遞時(shí)C#結(jié)構(gòu)體和類的區(qū)別,在程序中分別定義了表示員工的類classEmployee類和表示員工的結(jié)構(gòu)體structEmployee,并定義了各自的字段和構(gòu)造函數(shù)。在主程序入口Main方法中,聲明類的實(shí)例clsEmpA和clsEmpB,并使用構(gòu)造函數(shù)創(chuàng)建clsEmpA類實(shí)例,然后將clsEmpA類實(shí)例傳遞給clsEmpB類實(shí)例,修改clsEmpB類實(shí)例的字段值,最后打印clsEmpA類實(shí)例中的字段,查看字段的值是否隨clsEmpB類實(shí)例字段的修改而變化。同時(shí),聲明結(jié)構(gòu)體的實(shí)例strctEmpA和strctEmpB,并使用構(gòu)造函數(shù)創(chuàng)建strctEmpA結(jié)構(gòu)體實(shí)例,然后將strctEmpA結(jié)構(gòu)體實(shí)例傳遞給strctEmpB結(jié)構(gòu)體實(shí)例,修改strctEmpB結(jié)構(gòu)體實(shí)例的字段值,最后打印strctEmpA結(jié)構(gòu)體實(shí)例中的字段,查看字段的值是否隨strctEmpB結(jié)構(gòu)體實(shí)例字段的修改而變化。程序的流程圖如圖8.1所示。 

(2)C#結(jié)構(gòu)體和類的區(qū)別還有一個(gè)比較明顯的區(qū)別,就是類能夠定義默認(rèn)的、不帶參數(shù)的構(gòu)造函數(shù),并能在該構(gòu)造函數(shù)中初始化字段。而結(jié)構(gòu)體不允許定義默認(rèn)的、不帶參數(shù)的構(gòu)造函數(shù)。

C#結(jié)構(gòu)體和類的區(qū)別的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#結(jié)構(gòu)體和類的區(qū)別有所幫助。

【編輯推薦】

  1. C#結(jié)構(gòu)體的特點(diǎn)淺析
  2. C#結(jié)構(gòu)體數(shù)組間的轉(zhuǎn)化淺析
  3. 解決C#結(jié)構(gòu)體數(shù)組間的轉(zhuǎn)化
  4. C#結(jié)構(gòu)體使用淺析
  5. C#構(gòu)造函數(shù)介紹及分類淺析
責(zé)任編輯:仲衡 來(lái)源: 百度空間
相關(guān)推薦

2009-08-27 16:18:47

C#類C#結(jié)構(gòu)體

2009-08-13 11:18:50

C#結(jié)構(gòu)體

2009-08-13 13:29:04

C#結(jié)構(gòu)體使用

2009-08-13 14:56:46

C#的結(jié)構(gòu)體使用

2009-08-31 15:02:22

C#解析結(jié)構(gòu)體指針

2009-08-13 13:03:52

C#結(jié)構(gòu)體數(shù)組

2009-08-17 18:04:49

C# 枚舉

2009-08-21 17:24:06

C# SingleIn

2009-08-06 14:43:10

C# Calculat

2009-08-21 17:24:06

C# SingleIn

2009-08-21 11:31:59

異步和多線程的區(qū)別

2009-08-13 14:46:03

C#結(jié)構(gòu)體定義

2009-08-14 11:05:28

C#語(yǔ)言的結(jié)構(gòu)體

2009-08-27 13:37:11

C#類和結(jié)構(gòu)

2009-09-23 09:36:34

C#數(shù)組

2009-08-10 10:37:17

C#類與結(jié)構(gòu)

2009-08-13 14:24:44

C#結(jié)構(gòu)體構(gòu)造函數(shù)

2010-07-12 09:07:30

C#

2009-08-13 17:30:30

C#構(gòu)造函數(shù)

2009-08-13 15:03:58

C#結(jié)構(gòu)體變量
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)