偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C#結構體和類的區(qū)別淺析

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

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

C#結構體和類的區(qū)別技術要點:

◆類在傳遞的時候,傳遞的內容是位于托管內存中的位置,結構體在傳遞的時候,傳遞的內容是位于程序堆棧區(qū)的內容。當類的傳遞對象修改時,將同時修改源對象,而結構體的傳遞對象修改時,不會對源對象產(chǎn)生影響。

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

C#結構體和類的區(qū)別之實現(xiàn)步驟

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

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

  1. using System;   //C#結構體和類的區(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ù)的構造函數(shù)創(chuàng)建員工類的實例   
  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ù)的構造函數(shù)創(chuàng)建員工結構體的實例   
  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("結構體的數(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. //類的默認構造函數(shù),可以在類中重新定義   
  96.  
  97. public classEmployee()   
  98.  
  99. {   
  100.  
  101. firstname = "";   
  102.  
  103. lastname = "";   
  104.  
  105. age = 0;   
  106.  
  107. }   
  108.  //C#結構體和類的區(qū)別
  109. //類的帶參數(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//表示員工的結構體   
  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#結構體和類的區(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. //在結構體中不能定義默認的不帶參數(shù)的構造函數(shù),只能定義結構體的帶參數(shù)的構造函數(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鍵運行程序,運行結果如下所示。

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

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

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

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

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

C#結構體和類的區(qū)別的相關內容就向你介紹到這里,希望對你學習和了解C#結構體和類的區(qū)別有所幫助。

【編輯推薦】

  1. C#結構體的特點淺析
  2. C#結構體數(shù)組間的轉化淺析
  3. 解決C#結構體數(shù)組間的轉化
  4. C#結構體使用淺析
  5. C#構造函數(shù)介紹及分類淺析
責任編輯:仲衡 來源: 百度空間
相關推薦

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#結構體數(shù)組

2009-08-21 17:24:06

C# SingleIn

2009-08-17 18:04:49

C# 枚舉

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-14 11:05:28

C#語言的結構體

2009-08-13 14:46:03

C#結構體定義

2009-09-23 09:36:34

C#數(shù)組

2009-08-27 13:37:11

C#類和結構

2009-08-13 17:30:30

C#構造函數(shù)

2009-08-10 10:37:17

C#類與結構

2009-08-13 14:24:44

C#結構體構造函數(shù)

2010-07-12 09:07:30

C#

2009-08-26 09:54:45

C#打印預覽C#打印
點贊
收藏

51CTO技術棧公眾號