.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>