自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

解析C#關(guān)鍵字之get、set、value、partial、where和yield

開發(fā) 后端
本文詳細(xì)介紹了C#關(guān)鍵字get、set、value、partial、where和yield的用法,供大家參考。

上下文關(guān)鍵字用于提供代碼中的特定含義,但它不是 C# 中的保留字。

C#關(guān)鍵字——get、set、value

get 在屬性或索引器中定義“訪問器”方法,以檢索該屬性或該索引器元素的值。

set 義屬性或索引器中的“訪問器”方法,用于設(shè)置屬性或索引器元素的值。

value 隱式參數(shù),用于設(shè)置訪問器以及添加或移除事件處理程序。

  1. // 簡單示例  
  2. class Employee  
  3. {  
  4. private string _name;  
  5. public string Name  
  6.     {  
  7. get { return this._name;  }  
  8. set { this._name = value; }  
  9.     }  

注意:

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)和接口之一。

示例:

以下的部分類在編譯時,將會合并,包括它的方法、類型特性等等。 

  1. namespace Hunts.Keywords  
  2. {  
  3.     [System.Serializable]  
  4.     partial class Test  
  5.     {  
  6.         void Test1() { }  
  7.     }  
  8.     [Conditional("DEBUG")]  
  9.     partial class Test  
  10.     {  
  11.         void Test2() { }  
  12.     }  

類相當(dāng)于:

  1. namespace Hunts.Keywords  
  2. {  
  3.     [System.Serializable]  
  4.     [Conditional("DEBUG")]  
  5.     class Test  
  6.     {  
  7.         void Test1() { }  
  8.         void Test2() { }  
  9.     }  

關(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)用一個或多個約束獲得的。

  1. // 句法  
  2. public class MyGenericClass< T> where T:something 

something可以是:結(jié)構(gòu)、類、new()、< 基類名>或< 接口名稱>。

可以同時具有1中的多個約束,且約束自身也可以是泛型類型。

也可以將約束作用于泛型方法或委托。

要更深入的了解可以參閱MSDN Library中的“泛型編程”以及“類型參數(shù)的約束”這些內(nèi)容。

示例: 

  1.  // keywords_where.cs  
  2.    
  3.  using System;  
  4.  using System.Collections;  
  5.    
  6.  struct MyStruct  
  7.  {  
  8.      //...  
  9.  }  
  10.  
  11. interface IMyInterface  
  12. {  
  13.     //...  
  14. }  
  15.  
  16. class MyGenericClass< T1,T2>  
  17.     where T1: IEnumerable, IMyInterface  
  18.     where T2: MyStruct, new()  
  19. {  
  20.     public void MyMethod(T1 t1, T2 t2)  
  21.     {  
  22.         //...  
  23.     }  

C#關(guān)鍵字——yield

yield 在迭代器塊中用于向枚舉數(shù)對象提供值或發(fā)出迭代結(jié)束信號。 

  1. // expression 進(jìn)行計算并以枚舉數(shù)對象值的形式返回。expression 必須可以隱式轉(zhuǎn)換為迭代器的 yield 類型。  
  2. yield return expression;  
  3. yield break

示例:

  1. // keywords_yield.cs  
  2. using System;  
  3. using System.Collections;  
  4.  
  5. namespace Hunts.Keywords  
  6. {  
  7.     public class Employee  
  8.     {  
  9.         private string _name;  
  10.        private int _id;  
  11.  
  12.        public string Name  
  13.        {  
  14.            get { return this._name; }  
  15.            set { this._name = value; }  
  16.        }  
  17.  
  18.        public int ID  
  19.        {  
  20.            get { return this._id; }  
  21.            set { this._id = value; }  
  22.        }  
  23.  
  24.        // 為給定數(shù)組中的人名進(jìn)行編號  
  25.        public static IEnumerable SetIDs(string[] names)  
  26.        {  
  27.            int counter = 0;  
  28.            Employee result = new Employee();  
  29.            while (counter++ <  names.Length)  
  30.            {  
  31.                result._id = counter;  
  32.                result._name = names[counter - 1];  
  33.                yield return result;  
  34.            }  
  35.        }  
  36.    }  
  37.  
  38.    class EmployeeList  
  39.    {  
  40.        static void Main()  
  41.        {  
  42.            string[] names = { "Jones""Carl""Dennis""Evan""Hugo""Ivan" };  
  43.  
  44.            // 顯示編號操作的結(jié)果  
  45.            foreach (Employee e in Employee.SetIDs(names))  
  46.            {  
  47.                Console.WriteLine("ID:{0} Name:{1}", e.ID,e.Name);  
  48.            }  
  49.  
  50.            Console.Read();  
  51.        }  
  52.    }  

注意:

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 塊中。

【編輯推薦】

  1. 總結(jié)C#語言命名規(guī)范
  2. C#反射相關(guān)知識學(xué)習(xí)
  3. 大話F#和C#:是否會重蹈C#失敗的覆轍?
  4. 總結(jié)和學(xué)習(xí)C#接口
  5. 學(xué)習(xí)C#程序有感
責(zé)任編輯:book05 來源: cnblogs
相關(guān)推薦

2025-01-22 08:06:38

C#yield數(shù)據(jù)迭代

2009-12-18 11:37:54

Ruby關(guān)鍵字yiel

2009-07-31 16:34:17

dynamicC# 4.0

2009-08-21 14:58:56

C# this關(guān)鍵字

2013-01-30 10:12:14

Pythonyield

2009-09-02 09:24:03

C# this關(guān)鍵字

2009-08-06 17:52:23

C#增加that關(guān)鍵字

2009-08-13 17:44:34

C# using關(guān)鍵字

2009-08-21 14:47:59

C# base關(guān)鍵字

2009-08-26 15:16:29

C# lock關(guān)鍵字

2009-08-13 13:04:29

C# lock關(guān)鍵字

2009-08-21 14:16:35

C# New關(guān)鍵字

2009-09-10 16:38:43

C# get set用

2009-09-01 15:25:04

C# default關(guān)

2024-03-15 11:52:03

C++關(guān)鍵字編程

2023-12-11 13:59:00

YieldPython生成器函數(shù)

2009-09-11 09:15:32

C# get set

2024-12-26 00:28:59

C#base?關(guān)鍵字

2024-06-04 17:02:38

newC#編程語言

2009-12-17 13:57:15

Ruby關(guān)鍵字
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號