一、結(jié)構(gòu)與類的區(qū)別:
成都創(chuàng)新互聯(lián)公司從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元中站做網(wǎng)站,已為上家服務(wù),為中站各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108結(jié)構(gòu)與類共享大多數(shù)相同的語(yǔ)法,但結(jié)構(gòu)比類受到的限制更多:
1. 結(jié)構(gòu)是值類型,而類是引用類型。
2. 在結(jié)構(gòu)聲明中,除非字段被聲明為 const 或 static,否則無法初始化。
3. 結(jié)構(gòu)不能聲明默認(rèn)構(gòu)造函數(shù)(沒有參數(shù)的構(gòu)造函數(shù))或析構(gòu)函數(shù)。結(jié)構(gòu)可以聲明帶參數(shù)的構(gòu)造函數(shù)。
4. 一個(gè)結(jié)構(gòu)不能從另一個(gè)結(jié)構(gòu)或類繼承,而且不能作為一個(gè)類的基類。所有結(jié)構(gòu)都直接繼承自 System.ValueType,后者繼承自 System.Object。
5. 結(jié)構(gòu)可以實(shí)現(xiàn)接口。
6. 與類不同,結(jié)構(gòu)的實(shí)例化可以不使用 new 運(yùn)算符。
7. 結(jié)構(gòu)在賦值時(shí)進(jìn)行復(fù)制。 將結(jié)構(gòu)賦值給新變量時(shí),將復(fù)制所有數(shù)據(jù),并且對(duì)新副本所做的任何修改不會(huì)更改原始副本的數(shù)據(jù)。在使用值類型的集合(如 Dictionary<string, myStruct>)時(shí),請(qǐng)務(wù)必記住這一點(diǎn)。
8. 結(jié)構(gòu)可以為 null 的類型,因而可向其賦 null 值。
二、結(jié)構(gòu)體示例:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace CSharp.Struct
7: {
8:
9: public struct CoOrds //不能從另一個(gè)結(jié)構(gòu)或類繼承,但可實(shí)現(xiàn)接口
10: {
11: public int x, y;
12:
13: //結(jié)構(gòu)中不能有實(shí)例字段初始值,除非被聲明為 const 或 static
14: //public int x = 0;
15: //public int y = 0;
16:
17: //結(jié)構(gòu)不能聲明默認(rèn)構(gòu)造函數(shù)(沒有參數(shù)的構(gòu)造函數(shù))或析構(gòu)函數(shù)
18: //public CoOrds()
19: //{
20:
21: //}
22:
23: //結(jié)構(gòu)可以聲明帶參數(shù)的構(gòu)造函數(shù)。
24: public CoOrds(int p1, int p2)
25: {
26: x = p1;
27: y = p2;
28: }
29: }
30:
31: class Program
32: {
33: static void Main(string[] args)
34: {
35: //
36: //與類不同,結(jié)構(gòu)的實(shí)例化可以不使用 new 運(yùn)算符。
37: //
38:
39: // Declare an object:
40: CoOrds coords1;
41:
42: // Initialize:
43: coords1.x = 10;
44: coords1.y = 20;
45:
46: // Display results:
47: Console.Write("CoOrds 1: ");
48: Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
49:
50: // Initialize:
51: CoOrds coords2 = new CoOrds();
52: CoOrds coords3 = new CoOrds(10, 10);
53:
54: // Display results:
55: Console.Write("CoOrds 2: ");
56: Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
57:
58: Console.Write("CoOrds 3: ");
59: Console.WriteLine("x = {0}, y = {1}", coords3.x, coords3.y);
60:
61: //
62: //將結(jié)構(gòu)賦值給新變量時(shí),將復(fù)制所有數(shù)據(jù),并且對(duì)新副本所做的任何修改不會(huì)更改原始副本的數(shù)據(jù)。
63: //
64:
65: //Copy
66: Console.Write("After Copy:\n");
67: coords2 = coords3;
68: coords2.x = 20;
69: // Display results:
70: Console.Write("CoOrds 2: ");
71: Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
72:
73: Console.Write("CoOrds 3: ");
74: Console.WriteLine("x = {0}, y = {1}", coords3.x, coords3.y);
75:
76: // Keep the console window open in debug mode.
77: Console.WriteLine("Press any key to exit.");
78: Console.ReadKey();
79:
80: }
81: }
82: }
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
網(wǎng)站標(biāo)題:C#語(yǔ)言知識(shí)點(diǎn)整理-結(jié)構(gòu)-創(chuàng)新互聯(lián)
地址分享:http://muchs.cn/article36/cdchpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、品牌網(wǎng)站設(shè)計(jì)、服務(wù)器托管、軟件開發(fā)、用戶體驗(yàn)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容