c#語法特性
-
類(class)
-
理解類(一)
包含變量(屬性)
包含方法(行為)
繼承
用來創(chuàng)建對象
-
理解類(二)
核心是建模
-
面向?qū)ο?/p>
擅長設計
以對象為基礎單位
-
面向過程
擅長實現(xiàn)
以函數(shù)為基礎單位
-
理解類(三)
-
值類型
byte, short, int, float, double, decimal, char, bool, struct
直接存儲其值
-
引用類型
string, class
引用類型存儲對其值的引用
-
-
類的訪問權限(封裝性)
- internal, private, public, protected
類的命名
-
抽象類與接口(繼承性睛廊、多態(tài)性)
- 抽象類中有的時候需要些抽象方法匿垄,抽象方法需要再子類中覆寫,實現(xiàn)接口可以顯示實現(xiàn)和隱式實現(xiàn)
-
內(nèi)部類
- 有的時候需要在類內(nèi)部創(chuàng)建一些只需要在類內(nèi)部使用的對象汪茧,這時候可以用內(nèi)部類
partial關鍵字
-
泛型類
- 類需要適配不同的類型的時候廊谓,可以用泛型類舍咖,比如單例的模板许帐。
-
-
結構(struct)
是值類型
不能設置為 null
聲明變量時剿涮,本身就有值了
賦值時是深拷貝
不能局部賦值(比如 transofrm.position.x 不能直接賦值),作為一個屬性時腻异, struct 無法對單一的成員變量賦值进副,而 class 則是只要允許可以隨便賦值。
在結構聲明中悔常,除非將字段聲明為 const 或 static影斑,否則無法初始化。
結構不能聲明無參數(shù)構造函數(shù)(沒有參數(shù)的構造函數(shù))或終結器这嚣。
結構在分配時進行復制鸥昏。將結構分配給新變量時,將復制所有數(shù)據(jù)姐帚,并且對新副本所做的任何修改不會更改原始副本的數(shù)據(jù)吏垮。在處理值類型的集合(如
Dictionary<string, myStruct>
)時,請務必記住這一點罐旗。結構是值類型膳汪,不同于類,類是引用類型九秀。
與類不同遗嗽,無需使用
new
運算符即可對結構進行實例化。結構可以聲明具有參數(shù)的構造函數(shù)鼓蜒。
一個結構無法繼承自另一個結構或類痹换,并且它不能為類的基類。所有結構都直接繼承自ValueType都弹,后者繼承自Object娇豫。
結構可以實現(xiàn)接口。
結構不能為
null
畅厢,并且不能向結構變量分配null
冯痢,除非將變量聲明為可為空的值類型。
-
接口(interface)
-
基本描述
接口類似只有抽象成員的抽象基類框杜。實現(xiàn)接口的任何類火結構都必須實現(xiàn)其所有成員
接口無法直接進行實例化浦楣。其成員由實現(xiàn)接口的任何類或結構來實現(xiàn)。
接口可以包含事件咪辱、索引器振劳、方法和屬性。
接口不包含方法的實現(xiàn)油狂。
一個類或接口可以實現(xiàn)多個接口澎迎。一個類可以繼承一個基類庐杨,還可實現(xiàn)一個或多個接口
-
應用場景
官方的推薦場景是解決接口方法重名的問題
另一個應用場景是為了減少接口方法的訪問權限
-
事件(event)
屬性(property)
-
委托(delegate)
-
常用委托
Action<T>
Func<T>
-
注意事項
注冊委托和注銷委托最好成對出現(xiàn)
-
委托有可能為null,所以最好在生命委托變量時夹供,設置一個初始值,可以減少空指針異常的風險
- <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>
-
-
表達式
-
數(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>在表達式中使用i和text這些變量的時候仁堪,變量名稱計算為當前在改變量的內(nèi)存位置所儲存的值
-
調(diào)用表達式
- <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(){}
查詢表達式
-
Lambda表達式
- <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;}
-
語句
特性(有時候也叫屬性哮洽,Attribute)