.NET 開發基礎/使用集合
外觀
| .NET 開發基礎 | |
|---|---|
系統型別和集合:使用集合
考試目標:使用集合在 .NET Framework 應用程式中管理一組關聯資料。
(參考 System.Collections 名稱空間 - MSDN)
參見 MSDN
ArrayList 類用於大小會根據需要動態增加的陣列。ArrayList 不一定是排序的。
using System; using System.Collections;
public class Demo { public static void Main() { ArrayList myArrayList = new ArrayList(); myArrayList.Add("Testing"); myArrayList.Add("1...2...3"); } }
- ICollection 介面和 IList 介面
- ICollection 介面 - MSDN
- ICollection 介面是 System.Collections 名稱空間中類的基本介面。
- ICollection 介面擴充套件 IEnumerable;IDictionary 和 IList 是更專門的介面,它們擴充套件 ICollection。IDictionary 實現是鍵值對的集合,例如 Hashtable 類。IList 實現是值的集合,其成員可以透過索引訪問,例如 ArrayList 類。
- 一些限制對元素訪問的集合,例如 Queue 類和 Stack 類,直接實現 ICollection 介面。
- 如果 IDictionary 介面和 IList 介面都不滿足所需集合的要求,為了獲得更大的靈活性,從 ICollection 介面派生新的集合類。
- 下表列出了 ICollection 型別公開的成員。
- 公共屬性
Count - Gets the number of elements contained in the ICollection. IsSynchronized - Gets a value indicating whether access to the ICollection is synchronized (thread safe). SyncRoot - Gets an object that can be used to synchronize access to the ICollection.
- 公共方法
CopyTo - Copies the elements of the ICollection to an Array, starting at a particular Array index.
- IList 介面 - MSDN
- IComparer 介面、IEqualityComparer 介面和 IKeyComparer 介面
- IComparer 介面 - MSDN
- IEqualityComparer 介面 - MSDN
- IKeyComparer 介面 - IKeyComparer 在 .Net 2.0 中不存在
- IDictionary 介面和 IDictionaryEnumerator 介面
- IDictionary 介面 - MSDN
- IDictionaryEnumerator 介面 - MSDN
C# 程式碼示例
IEnumerator 示例
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
//Enumerators are positioned before the first element
//until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}
public IEnumerator GetEnumerator()
{
return new PeopleEnum(_people);
}
}
寫下用於練習以上程式碼的處理程式。
protected void lnkEnumerator_Click(object sender, EventArgs e)
{
Person[] peopleArray = new Person[] {
new Person("Irfan", "Akhtar"),
new Person("Hammad", "Anwar"),
new Person("Majid", "Aalim") };
PeopleEnum Prson = new PeopleEnum(peopleArray);
- 一種使用 IEnumerator 的方法。
while (Prson.MoveNext () )
{
Person P = (Person)Prson.Current;
Response.Write("First Name : " + P.firstName + ", Last Name : " + P.lastName);
}
- 一種使用 IEnumerable 的方法。
People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Response.Write("First Name : " + p.firstName + ", Last Name : " + p.lastName);
- IHashCodeProvider 介面 - MSDN - 此介面現在已過時(從 .NET 2.0 開始)
參見 MSDN
- 迭代器實際上是 IEnumerable 介面的輕量級版本。它主要與 foreach 語句一起使用。
- 通常會實現 IEnumerable 介面的 GetEnumerator 方法。
public class Colors : System.Collections.IEnumerable { string[] colors = { "Red", "Green", "Blue" };
public System.Collections.IEnumerator GetEnumerator() { for (int i = 0; i < colors.Length; i++) { yield return colors[i]; } } }
- 這使得可以使用標準 foreach 語句訪問該類。一個類不限於只實現一個迭代器。可以提供多個迭代器,例如允許按列表的升序和降序進行迭代。要呼叫命名迭代器,請使用以下語法
foreach (int i in myList.NamedIterator()) { System.Console.WriteLine(i); }
- yield 語句標記了迭代器執行將在後續迭代中恢復的點。這可以用於提供多個 yield 語句
public System.Collections.IEnumerator GetEnumerator() { yield return "Statement returned on iteration 1"; yield return "Statement returned on iteration 2"; }
- 要以程式設計方式結束迭代,請使用
yield break;
- 語句。
Hashtable 類 - MSDN
- 用於表示鍵值對的集合。
CollectionBase 類和 ReadOnlyCollectionBase 類
- CollectionBase 類 - MSDN
- ReadOnlyCollectionBase 類 - MSDN
DictionaryBase 類和 DictionaryEntry 類
- DictionaryBase 類 - MSDN
- DictionaryEntry 結構 - MSDN
Comparer 類 - MSDN
Queue 類 - MSDN
SortedList 類 - MSDN
BitArray 類 - MSDN
Stack 類 - MSDN
<noinclide>