C#結(jié)構(gòu)體和類的區(qū)別淺析
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文件,代碼如下所示。
- using System; //C#結(jié)構(gòu)體和類的區(qū)別
- using System.Collections.Generic;
- using System.Text;
- namespace ClassAndStruct
- {
- class Program
- {
- static void Main(string[] args)
- {
- //使用帶參數(shù)的構(gòu)造函數(shù)創(chuàng)建員工類的實(shí)例
- classEmployee clsEmpA = new
- classEmployee("Pony","Smith",43);
- classEmployee clsEmpB = clsEmpA;
- //修改引用數(shù)據(jù)
- clsEmpB.Age = 33;
- //使用帶參數(shù)的構(gòu)造函數(shù)創(chuàng)建員工結(jié)構(gòu)體的實(shí)例
- structEmployee strctEmpA =
- new structEmployee("Pony", "Smith", 43);
- structEmployee strctEmpB = strctEmpA;
- //修改
- strctEmpB.Age = 33;
- Console.WriteLine("類的數(shù)據(jù):姓名-{0} {1} 年齡-{2}",
- clsEmpA.FirstName,clsEmpA.LastName,clsEmpA.Age);
- Console.WriteLine("結(jié)構(gòu)體的數(shù)據(jù):姓名-{0} {1} 年齡-{2}",
- strctEmpA.FirstName, strctEmpA.LastName, strctEmpA.Age);
- Console.ReadLine();
- }
- }
- class classEmployee//表示員工的類
- {
- private string firstname;
- public string FirstName
- {
- get { return firstname; }
- set { firstname = value; }
- }
- private string lastname;
- public string LastName
- {
- get { return lastname; }
- set { lastname = value; }
- }
- private int age;
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- //類的默認(rèn)構(gòu)造函數(shù),可以在類中重新定義
- public classEmployee()
- {
- firstname = "";
- lastname = "";
- age = 0;
- }
- //C#結(jié)構(gòu)體和類的區(qū)別
- //類的帶參數(shù)的構(gòu)造函數(shù),在構(gòu)造類實(shí)例的同時(shí)給字段賦值
- public classEmployee(
- string strFirstNamem, string strLastName, int iAge)
- {
- firstname = strFirstNamem;
- lastname = strLastName;
- age = iAge;
- }
- }
- struct structEmployee//表示員工的結(jié)構(gòu)體
- {
- private string firstname;
- public string FirstName
- {
- get { return firstname; }
- set { firstname = value; }
- }
- private string lastname;
- public string LastName
- {
- get { return lastname; }
- set { lastname = value; }
- }
- //C#結(jié)構(gòu)體和類的區(qū)別
- private int age;
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- //在結(jié)構(gòu)體中不能定義默認(rèn)的不帶參數(shù)的構(gòu)造函數(shù),只能定義結(jié)構(gòu)體的帶參數(shù)的構(gòu)造函數(shù)
- public structEmployee(string strFirstNamem,
- string strLastName, int iAge)
- {
- firstname = strFirstNamem;
- lastname = strLastName;
- age = iAge;
- }
- }
- }
(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ū)別有所幫助。
【編輯推薦】