

























发展:数组-->集合-->泛型
(1)数组
(2)集合
1. 针对数组数据结构的缺点,我们使用集合数据结构.
2. 集合数据结构中的类都位于System.Collections命名空间中.
3. 说到集合,我们必须先了解几个接口.想具体了解以下接口,可参考(3)集合接口
3.1 IEnumerable接口和IEnumerator接口
3.2 ICollection接口
public interface ICollection : IEnumerable
3.3 IList接口
public interface IList : ICollection, IEnumerable
3.4 IDictionary接口
public interface IDictionary : ICollection, IEnumerable
4. 说明一下:
4.1 ICollection接口是System.Collections命名空间中类的基接口.
4.2 ICollection接口扩展IEnumerable;IDictionary和IList则是扩展ICollection的更为专用的接口.IDictionary实现是键/值对的集合,如Hashtable类.IList实现是值的集合,其成员可通过索引访问,如ArrayList类.
4.3 某些集合(如Queue类和Stack类)限制对其元素的访问,它们直接实现ICollection接口.
4.4 如果IDictionary接口和IList接口都不能满足所需集合的要求,则从ICollection接口派生新集合类以提高灵活性.
(3)IEnumerable接口和IEnumerator接口
1. 我的理解:只有实现了IEnumerable接口的数据结构类才能使用foreach语句,下面给出例子
//Person类
public class Person
{
private string _firstName;
private string _lastName;
public string FirstName
{
get { return _firstName; }
}
public string LastName
{
get { return _lastName; }
}
public Person(string strFirstName, string strLastName)
{
this._firstName = strFirstName;
this._lastName = strLastName;
}
}
//PersonList集合,实现IEnumerable接口
public class PersonList : IEnumerable
{
private Person[] _arrPerson;
public PersonList(Person[] myArrPerson)
{
_arrPerson = new Person[myArrPerson.Length];
for (int i = 0; i < myArrPerson.Length; i++)
{
_arrPerson[i] = myArrPerson[i];
}
}
public IEnumerator GetEnumerator()
{
return new PeopleEnumerator(_arrPerson);
}
}
//实现IEnumerator接口
public class PeopleEnumerator : IEnumerator
{
private int _position = -1;
public Person[] _arrPerson;
public object Current
{
get
{
try
{
return _arrPerson[_position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public PeopleEnumerator(Person[] myArrPerson)
{
_arrPerson = myArrPerson;
}
public bool MoveNext()
{
_position++;
return (_position < _arrPerson.Length);
}
public void Reset()
{
_position = -1;
}
}
//集合的使用
protected void Page_Load(object sender, EventArgs e)
{
Person[] myArrPerson = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
PersonList myPersonList = new PersonList(myArrPerson);
foreach (Person myPerson in myPersonList)
{
lblName.Text += myPerson.FirstName + " " + myPerson.LastName + ",";
}
}
6. 说明一下我的理解,定义了一个集合类,实现IEnumerable接口,则必须定义一个与之相应的实现IEnumerator接口的类,这样是不是很麻烦呢?
(4)迭代器
1. 使用迭代器可避免上述麻烦,修改代码,注意橙色部分
public class Person
{
private string _firstName;
private string _lastName;
public string FirstName
{
get { return _firstName; }
}
public string LastName
{
get { return _lastName; }
}
public Person(string strFirstName, string strLastName)
{
this._firstName = strFirstName;
this._lastName = strLastName;
}
}
public class PersonList : IEnumerable
{
private Person[] _arrPerson;
public PersonList(Person[] myArrPerson)
{
_arrPerson = new Person[myArrPerson.Length];
for (int i = 0; i < myArrPerson.Length; i++)
{
_arrPerson[i] = myArrPerson[i];
}
}
public IEnumerator GetEnumerator()
{
//当编译器检测到迭代器时,它将自动生成IEnumerable或IEnumerable<T>接口的Current,MoveNext和Dispose方法.
for (int i = 0; i < _arrPerson.Length; i++)
{
yield return _arrPerson[i];
}
}
}
2. 深入研究迭代器,请见(4)迭代器
3. 参考:http://msdn2.microsoft.com/zh-cn/library/dscyy5s0(VS.80).aspx
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。