迭代器

(1)數組
1. 數組數據結構是System.Array類的一個實例.
2. System.Array類的語法爲
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Array : ICloneable, IList, ICollection, IEnumerable
3. 下面看一個使用數組的例子(我稱之爲隱式實現)
protected void Page_Load(object sender, EventArgs e)
{
    string[] strArrName = new string[3] { "Jim", "Andy", "Sun" };
    foreach (string strName in strArrName)
    {
        lblName.Text += strName + ",";
    }
}
或者這樣寫
protected void Page_Load(object sender, EventArgs e)
{
    string[] strArrName = new string[3];
    strArrName[0] = "Jim";
    strArrName[1] = "Andy";
    strArrName[2] = "Sun";
    foreach (string strName in strArrName)
    {
        lblName.Text += strName + ",";
    }
}
顯示結果如下
Jim,Andy,Sun,
4. 下面看另一個使用數組的例子(我稱之爲顯式實現)
protected void Page_Load(object sender, EventArgs e)
{
    Array myArray = Array.CreateInstance(typeof(string), 3);
    myArray.SetValue("Jim", 0);
    myArray.SetValue("Andy", 1);
    myArray.SetValue("Sun", 2);
    foreach (string strName in myArray)
    {
        lblName.Text += strName + ",";
    }
}
顯示結果如下
Jim,Andy,Sun,
5. 優點:可以高效的訪問給定下標的元素;System.Array類有自己的C#語法,使用它編程非常的直觀.
6. 缺點:在實例化時必須指定數組的大小,以後也不能添加,插入,刪除元素.

(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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章