c#語(yǔ)法特性
-
類(class)
-
理解類(一)
包含變量(屬性)
包含方法(行為)
繼承
用來創(chuàng)建對(duì)象
-
理解類(二)
核心是建模
-
面向?qū)ο?/p>
擅長(zhǎng)設(shè)計(jì)
以對(duì)象為基礎(chǔ)單位
-
面向過程
擅長(zhǎng)實(shí)現(xiàn)
以函數(shù)為基礎(chǔ)單位
-
理解類(三)
-
值類型
byte, short, int, float, double, decimal, char, bool, struct
直接存儲(chǔ)其值
-
引用類型
string, class
引用類型存儲(chǔ)對(duì)其值的引用
-
-
類的訪問權(quán)限(封裝性)
- internal, private, public, protected
類的命名
-
抽象類與接口(繼承性、多態(tài)性)
- 抽象類中有的時(shí)候需要些抽象方法,抽象方法需要再子類中覆寫,實(shí)現(xiàn)接口可以顯示實(shí)現(xiàn)和隱式實(shí)現(xiàn)
-
內(nèi)部類
- 有的時(shí)候需要在類內(nèi)部創(chuàng)建一些只需要在類內(nèi)部使用的對(duì)象,這時(shí)候可以用內(nèi)部類
partial關(guān)鍵字
-
泛型類
- 類需要適配不同的類型的時(shí)候,可以用泛型類,比如單例的模板。
-
-
結(jié)構(gòu)(struct)
是值類型
不能設(shè)置為 null
聲明變量時(shí),本身就有值了
賦值時(shí)是深拷貝
不能局部賦值(比如 transofrm.position.x 不能直接賦值),作為一個(gè)屬性時(shí), struct 無法對(duì)單一的成員變量賦值,而 class 則是只要允許可以隨便賦值。
在結(jié)構(gòu)聲明中,除非將字段聲明為 const 或 static,否則無法初始化。
結(jié)構(gòu)不能聲明無參數(shù)構(gòu)造函數(shù)(沒有參數(shù)的構(gòu)造函數(shù))或終結(jié)器。
結(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)。結(jié)構(gòu)是值類型,不同于類,類是引用類型。
與類不同,無需使用
new運(yùn)算符即可對(duì)結(jié)構(gòu)進(jìn)行實(shí)例化。結(jié)構(gòu)可以聲明具有參數(shù)的構(gòu)造函數(shù)。
一個(gè)結(jié)構(gòu)無法繼承自另一個(gè)結(jié)構(gòu)或類,并且它不能為類的基類。所有結(jié)構(gòu)都直接繼承自ValueType,后者繼承自Object。
結(jié)構(gòu)可以實(shí)現(xiàn)接口。
結(jié)構(gòu)不能為
null,并且不能向結(jié)構(gòu)變量分配null,除非將變量聲明為可為空的值類型。
-
接口(interface)
-
基本描述
接口類似只有抽象成員的抽象基類。實(shí)現(xiàn)接口的任何類火結(jié)構(gòu)都必須實(shí)現(xiàn)其所有成員
接口無法直接進(jìn)行實(shí)例化。其成員由實(shí)現(xiàn)接口的任何類或結(jié)構(gòu)來實(shí)現(xiàn)。
接口可以包含事件、索引器、方法和屬性。
接口不包含方法的實(shí)現(xiàn)。
一個(gè)類或接口可以實(shí)現(xiàn)多個(gè)接口。一個(gè)類可以繼承一個(gè)基類,還可實(shí)現(xiàn)一個(gè)或多個(gè)接口
-
應(yīng)用場(chǎng)景
官方的推薦場(chǎng)景是解決接口方法重名的問題
另一個(gè)應(yīng)用場(chǎng)景是為了減少接口方法的訪問權(quán)限
-
事件(event)
屬性(property)
-
委托(delegate)
-
常用委托
Action<T>
Func<T>
-
注意事項(xiàng)
注冊(cè)委托和注銷委托最好成對(duì)出現(xiàn)
-
委托有可能為null,所以最好在生命委托變量時(shí),設(shè)置一個(gè)初始值,可以減少空指針異常的風(fēng)險(xiǎn)
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c#" cid="n155" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">public Action<int> OnAgeChanged = (age)=>{}</pre>
-
-
表達(dá)式
-
數(shù)值和字符串
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c#" cid="n180" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">int i = 5;
string text = "hello world";
?
Debug.Log(i);
Debug.Log(text);</pre>在表達(dá)式中使用i和text這些變量的時(shí)候,變量名稱計(jì)算為當(dāng)前在改變量的內(nèi)存位置所儲(chǔ)存的值
-
調(diào)用表達(dá)式
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c#" cid="n187" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">void DoWork(){}
DoWork();
?
Action SomeAction = ()=>{}
SomeAction();</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c#" cid="n187" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">void DoWork(){}
查詢表達(dá)式
-
Lambda表達(dá)式
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c#" cid="n220" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">(paramA)=>{return someValue;}
() => {}
(paramA)=>someValue</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c#" cid="n220" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">(paramA)=>{return someValue;}
-
語(yǔ)句
特性(有時(shí)候也叫屬性,Attribute)