解析C#關(guān)鍵字之get、set、value、partial、where和yield
上下文關(guān)鍵字用于提供代碼中的特定含義,但它不是 C# 中的保留字。
C#關(guān)鍵字——get、set、value
get 在屬性或索引器中定義“訪問器”方法,以檢索該屬性或該索引器元素的值。
set 義屬性或索引器中的“訪問器”方法,用于設(shè)置屬性或索引器元素的值。
value 隱式參數(shù),用于設(shè)置訪問器以及添加或移除事件處理程序。
- // 簡單示例
- class Employee
- {
- private string _name;
- public string Name
- {
- get { return this._name; }
- set { this._name = value; }
- }
- }
注意:
get、set“訪問器”,在默認(rèn)情況下具有相同的訪問級別。但是有時鑒于對讀寫的考慮,通??梢韵拗苨et的訪問級別。對屬性或索引器使用訪問修飾符受以下條件的制約:
不能對接口或顯式接口成員實(shí)現(xiàn)使用訪問器修飾符。
僅當(dāng)屬性或索引器同時具有 set 和 get 訪問器時,才能使用訪問器修飾符。這種情況下,只允許對其中一個訪問器使用修飾符。
如果屬性或索引器具有 override 修飾符,則訪問器修飾符必須與重寫的訪問器的訪問器(如果有的話)匹配。
訪問器的可訪問性級別必須比屬性或索引器本身的可訪問性級別具有更嚴(yán)格的限制。
C#關(guān)鍵字——partial
partial 分部類型定義允許將類、結(jié)構(gòu)或接口的定義拆分到多個文件中。
[modifiers] partial type
modifiers是可選的??梢允莂bstract、new、override、static、virtual、extern,以及訪問修飾符中的一個。
type 可以是類、結(jié)構(gòu)和接口之一。
示例:
以下的部分類在編譯時,將會合并,包括它的方法、類型特性等等。
- namespace Hunts.Keywords
- {
- [System.Serializable]
- partial class Test
- {
- void Test1() { }
- }
- [Conditional("DEBUG")]
- partial class Test
- {
- void Test2() { }
- }
- }
類相當(dāng)于:
- namespace Hunts.Keywords
- {
- [System.Serializable]
- [Conditional("DEBUG")]
- class Test
- {
- void Test1() { }
- void Test2() { }
- }
關(guān)于部分(類、接口、結(jié)構(gòu))的詳細(xì)使用,可以參閱MSDN Library中的部分類。
C#關(guān)鍵字——where
where 子句用于指定類型約束,這些約束可以作為泛型聲明中定義的類型參數(shù)的變量。
之所以使用類型約束是因?yàn)槿绻獧z查泛型列表中的某個項(xiàng)以確定它是否有效,或者將它與其他某個項(xiàng)進(jìn)行比較,則編譯器必須在一定程度上保證它需要調(diào)用的運(yùn)算符或方法將受到客戶端代碼可能指定的任何類型參數(shù)的支持。這種保證是通過對泛型類定義應(yīng)用一個或多個約束獲得的。
- // 句法
- public class MyGenericClass< T> where T:something
something可以是:結(jié)構(gòu)、類、new()、< 基類名>或< 接口名稱>。
可以同時具有1中的多個約束,且約束自身也可以是泛型類型。
也可以將約束作用于泛型方法或委托。
要更深入的了解可以參閱MSDN Library中的“泛型編程”以及“類型參數(shù)的約束”這些內(nèi)容。
示例:
- // keywords_where.cs
- using System;
- using System.Collections;
- struct MyStruct
- {
- //...
- }
- interface IMyInterface
- {
- //...
- }
- class MyGenericClass< T1,T2>
- where T1: IEnumerable, IMyInterface
- where T2: MyStruct, new()
- {
- public void MyMethod(T1 t1, T2 t2)
- {
- //...
- }
- }
C#關(guān)鍵字——yield
yield 在迭代器塊中用于向枚舉數(shù)對象提供值或發(fā)出迭代結(jié)束信號。
- // expression 進(jìn)行計算并以枚舉數(shù)對象值的形式返回。expression 必須可以隱式轉(zhuǎn)換為迭代器的 yield 類型。
- yield return expression;
- yield break;
示例:
- // keywords_yield.cs
- using System;
- using System.Collections;
- namespace Hunts.Keywords
- {
- public class Employee
- {
- private string _name;
- private int _id;
- public string Name
- {
- get { return this._name; }
- set { this._name = value; }
- }
- public int ID
- {
- get { return this._id; }
- set { this._id = value; }
- }
- // 為給定數(shù)組中的人名進(jìn)行編號
- public static IEnumerable SetIDs(string[] names)
- {
- int counter = 0;
- Employee result = new Employee();
- while (counter++ < names.Length)
- {
- result._id = counter;
- result._name = names[counter - 1];
- yield return result;
- }
- }
- }
- class EmployeeList
- {
- static void Main()
- {
- string[] names = { "Jones", "Carl", "Dennis", "Evan", "Hugo", "Ivan" };
- // 顯示編號操作的結(jié)果
- foreach (Employee e in Employee.SetIDs(names))
- {
- Console.WriteLine("ID:{0} Name:{1}", e.ID,e.Name);
- }
- Console.Read();
- }
- }
注意:
yield 語句只能出現(xiàn)在 iterator 塊中,該塊可用作方法、運(yùn)算符或訪問器的體。這類方法、運(yùn)算符或訪問器的體受以下約束的控制:
不允許不安全塊。
方法、運(yùn)算符或訪問器的參數(shù)不能是 ref 或 out。
yield 語句不能出現(xiàn)在匿名方法中。
當(dāng)和 expression 一起使用時,yield return 語句不能出現(xiàn)在 catch 塊中或含有一個或多個 catch 子句的 try 塊中。
【編輯推薦】