索引器,枚举器,迭代器,比较器

索引器实现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方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值