9.4 集合介面
.NET Framework為集合的枚舉和對比提供了兩組標準介面:傳統的(非型別安全)和新的泛型型別安全集合。本書只聚焦於新的,型別安全的集合介面,因為它更優越。
您可以聲明一些指定了類型的ICollection來取代實際類型(如int或string),以用於介面聲明中的泛型型別(<T>)。
C++程式員需要注意:C#泛型和C++中的模板在文法和用法上都很相似。但是,因為泛型在運行期為其指定類型進行擴充,JIT編譯器可以為它們的不同的執行個體共用一段代碼,從而使得在使用C++模板時有可能出現的代碼膨脹問題在這裡顯著地減少。
關鍵的泛型集合介面列於表9-2:
For backward compatibility, C# also provides nongeneric interfaces (e.g., ICollection, IEnumerator), but they aren't considered here because they are obsolescent.
為了向後相容,C#也提供了非泛型介面(如IConnection,IEnumerator),但由於慢慢被棄用,這裡並不列出他們。
表9-2 集合介面
| 介面 |
用途 |
| ICollection<T> |
泛型集合的基底介面 |
IEnumerator<T>
IEnumerable<T> |
使用foreach語句枚舉整個集合 |
| ICollection<T> |
被所有集合實現以提供CopyTo()方法,Count,IsSynchronized和SyncRoot屬性 |
IComparer<T>
IComparable<T> |
對比集合中的兩個對象,使得集合可以被排序 |
| IList<T> |
象數組一樣使用索引訪問集合 |
| IDictionary<K,V> |
在基於鍵/值的集合中使用,如Dictionary |
9.4.1 IEnumerable<T>介面
您可以通過實現IEnumerable<T>介面來使得ListBoxTest支援foreach語句(見例9-11)。IEnumerable只有一個方法:GetEnumerator(),它的工作是返回一個實現了IEnumerator<T>介面的類。C#語言使用一個新的關鍵字yield來為建立列舉程式(enumerator)提供特殊的協助。
例9-11 建立ListBox的可枚舉類
using System;
using System.Collections; //譯者註:這句原文沒有,必須添加
using System.Collections.Generic;
using System.Text;
namespace Enumerable
{
public class ListBoxTest : IEnumerable<string>
{
private string[] strings;
private int ctr = 0;
//Enumerable類可以返回一個列舉程式
public IEnumerator<string> GetEnumerator()
{
foreach (string s in strings)
yield return s;
}
/*譯者註:泛型IEnumerable的定義為
*public interface IEnumerable<T> : IEnumerable
* 也就是說,在實現泛型版IEnumerable的同時還必須同時實現
* 非泛型版的IEnumerable介面,原文代碼並沒有這個內容,下面的三行
* 代碼是我添加進去的以使得代碼可以直接拷貝並運行*/
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
//使用字串數組來初始化ListBox
public ListBoxTest(params string[] initialStrings)
{
//為strings分配空間
strings = new string[8];
//拷貝從構造方法傳遞進來的字串數組
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
//在ListBox未尾添加一個字串
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
//允許象數組一樣訪問,其實就是索引器,如果對索引器有不明白的請訪問:
//http://www.enet.com.cn/eschool/video/c/20.shtml
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
//處理錯誤的索引
//譯者註:原文這裡沒加代碼,我加了一個異常下去
throw new ArgumentOutOfRangeException("索引", "索引超出範圍");
}
return strings[index];
}
set
{
strings[index] = value;
}
}
//獲得擁有字串的數量
public int GetNumEneries()
{
return ctr;
}
}
public class Tester
{
static void Main()
{
//建立一個新的ListBox並初始化
ListBoxTest lbt = new ListBoxTest("Hello", "World");
//添加一些字串
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");
//訪問測試
string subst = "Universe";
lbt[1] = subst;
//列出所有字串
foreach (string s in lbt)
{
Console.WriteLine("Value: {0}", s);
}
}
}
}