索引器,枚舉器,迭代器,比較器

索引器實現for循環

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            People people = new People();
            people[0] = "a";
            people[1] = "b";
            for (int i = 0; i < people.Count; i++)
            {
                Console.WriteLine(people[i]);
            }
            Console.ReadKey();
        }     
    }
    public class People
    {
        Hashtable hash = new Hashtable();
        public int Count
        {
            get { return hash.Keys.Count; }
        }
        //索引器
        public string this[int index]
        {
            get { return hash[index].ToString(); }
            set { hash.Add(index, value); }
        }
    }
}

枚舉器實現foreach

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            People people = new People();
            people[0] = "a";
            people[1] = "b";
            for (int i = 0; i < people.Count; i++)
            {
                Console.WriteLine(people[i]);
            }
            foreach (string p in people)
            {
                Console.WriteLine(p);
            }
            Console.ReadKey();
        }     
    }
    public class People : IEnumerable
    {
        Hashtable hash = new Hashtable();

        public int Count
        {
            get { return hash.Keys.Count; }
        }
        //索引器
        public string this[int index]
        {
            get { return hash[index].ToString(); }
            set { hash.Add(index, value); }
        }

        // 類裏有GetEnumerator()就能實現foreach,可以不繼承 IEnumerable 接口
        public IEnumerator GetEnumerator()
        {
            return new PeopleEnumerator(hash);//返回枚舉器
        }
        其實有下面這個迭代器就可以了,用yield return,編譯器會幫忙生成枚舉器
        //public IEnumerator GetEnumerator()
        //{
        //    for (int i = 0; i < hash.Keys.Count; i++)
        //    {
        //        yield return hash[i].ToString();
        //    }
        //}
    }

    //枚舉器
    public class PeopleEnumerator : IEnumerator
    {
        private Hashtable hash;
        private int index = -1;
        public PeopleEnumerator(Hashtable _hash)
        {
            hash = _hash;
        }
        public object Current
        {
            get
            {
                if (index > -1 || index < hash.Keys.Count)
                {
                    return hash[index].ToString();
                }
                else
                {
                    return new IndexOutOfRangeException();
                }
            }
        }

        public bool MoveNext()
        {
            if (index + 1 < hash.Keys.Count)
            {
                index++;
                return true;
            }
            index = -1;
            return false;
        }

        public void Reset()
        {
            index = -1;
        }
    }
}

** IEnumerator泛型接口;foreach中var能否類型推斷的解釋**

如圖,將上面代碼的foreach (string p in people)中的string改爲var,推斷var爲object類型
這裏寫圖片描述
這是枚舉中代碼

 public object Current
        {
            get
            {
                if (index > -1 || index < hash.Keys.Count)
                {
                    return hash[index].ToString();
                }
                else
                {
                    return new IndexOutOfRangeException();
                }
            }
        }

的返回是object

枚舉器實現IEnumerator<T>接口就可以使var能推斷出想要的類型了,主要就兩點變化,見註釋
代碼如下

   
     public class People/*:IEnumerable<string>*/
    {
        Hashtable hash = new Hashtable();

        public int Count
        {
            get { return hash.Keys.Count; }
        }
        
        public string this[int index]
        {
            get { return hash[index].ToString(); }
            set { hash.Add(index, value); }
        }

        // 這個類就不繼承IEnumerator<string>接口了,GetEnumerator()方法直接返回IEnumerator<string>就OK,如果繼承IEnumerator<string>,因爲IEnumerator<string>還繼承了IEnumerator,還得多寫兩個顯示實現的方法
        public IEnumerator<string> GetEnumerator()
        {
             return new PeopleEnumerator(hash);
        }
          
        //IEnumerator<string> IEnumerable<string>.GetEnumerator()
        //{
        //    throw new NotImplementedException();
        //}

        //IEnumerator IEnumerable.GetEnumerator()
        //{
        //    throw new NotImplementedException();
        //}
    }
    //枚舉器得繼承泛型接口
    public class PeopleEnumerator : IEnumerator<string>
    {
        private Hashtable hash;
        private int index = -1;
        public PeopleEnumerator(Hashtable _hash)
        {
            hash = _hash;
        }

        public string Current
        {
            get
            {
                return hash[index].ToString();
            }
        }

        object IEnumerator.Current
        {
            get
            {
                return hash[index].ToString();
            }
        }

        public void Dispose()
        {

        }

        public bool MoveNext()
        {
            if (index + 1 < hash.Keys.Count)
            {
                index++;
                return true;
            }
            index = -1;
            return false;
        }

        public void Reset()
        {
            index = -1;
        }
    }

顯示實現接口可以參考:
http://blog.csdn.net/m0_38110784/article/details/78040912

PS:HashTable的foreach裏的var不能類型推斷,Dictionary的可以就是這個原因

,所以HashTable一般 foreach (DictionaryEntry h in hash){},這樣h才能 . 出Key。
而Dictionary 用 foreach(var d in dic ) ,d就能 . 出Key。

這裏寫圖片描述
** IComparable<T> 比較器**
實現IComparable<T>接口的CompareTo方法。

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