IEnumerable、GetEnumerator、IEnumerator之间的关系



一、IEnumerator

 

该接口定义需要实现以下方法:

1、Current:获取集合中的当前元素。

2、MoveNext:将枚举数推进到集合的下一个元素。

3、Reset: 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。

实现以上方法的类也就意味着这个集合类能够被遍历访问。

 

 

二、IEnumerable

 

able意味着这个类能够达到上面IEnumerator的效果, 事实上也是如此,可以从这个接口定义方法中看出来:

GetEnumerator: 返回一个IEnumerator类型的值

IEnumerable是集合类的基础, 它解决了集合类如何遍历的问题, 所以更高级的集合类和接口都是继承IEnumerable

所有能够使用foreach遍历的集合类,都必须继承IEnumerable接口

 

三、ICollection

 

ICollection接口扩展IEnumerable

这个接口是很多集合类的继承接口,定义了3个属性和一个方法

 

1、只读属性

Count:集合中元素的数目

IsSynchronized: 是否同步对集合的访问

SyncRoot: 用来控制集合同步的对象

 

2、方法

CopyTo: 从特定的索引开始复制集合的元素到数组

 

 

四、IList

IList 是 ICollection 接口的子代,并且是所有非泛型列表的基接口。
IList 实现有三种类别:只读、固定大小和可变大小。无法修改只读 IList。固定大小的 IList 不允许添加或移除元素,但允许修改现有元素。可变大小的 IList 允许添加、移除和修改元素。

 

五、IDictionary


每个元素都是一个存储在 DictionaryEntry 对象中的键/值对。
每一对都必须有唯一的键。实现在是否允许键为空引用方面有所不同。此值可以为空引用,并且不必是唯一的。IDictionary 接口允许对所包含的键和值进行枚举,但这并不意味着任何特定的排序顺序。
IDictionary 实现有三种类别:只读、固定大小、可变大小。无法修改只读 IDictionary 对象。固定大小的 IDictionary 对象不允许添加或移除元素,但允许修改现有元素。可变大小的 IDictionary 对象允许添加、移除和修改元素。


C# 语言中的 foreach 语句需要集合中每个元素的类型。由于 IDictionary 对象的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 DictionaryEntry 类型

 

 

六、IEnumerable、GetEnumerator、IEnumerator之间的关系

 

1. 一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回IEnumerator对象)。 
 
2. IEnumerator对象具体实现了iterator(通过MoveNext(),Reset(),Current)。 
 
3. 从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举(enumerable)”的,但并没有说明如何实现枚举器(iterator)IEnumerator是一个实现式的接口,IEnumerator对象就是一个iterator(迭代器)。 
 
4. IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,可以通过IEnumerable的GetEnumerator()得到IEnumerator对象。
 
5. 由于IEnumerable<T>扩展(继承)了旧的IEnumerable接口,所以要实现两个不同的方法:
IEnumerator<T> GetEnumerator();
IEnumerator GetEnumerator(); // 由于和泛型版本的方法同名,所以该方法的实现需要使用显式接口实现




namespace ForeachTestCase
{
      //继承IEnumerable接口,其实也可以不继承这个接口,只要类里面含有返回IEnumberator引用的GetEnumerator()方法即可
    class ForeachTest:IEnumerable     {
        private string[] elements;  //装载字符串的数组
        private int ctr = 0;  //数组的下标计数器

        /** <summary>
        /// 初始化的字符串
        /// </summary>
        /// <param name="initialStrings"></param>
        ForeachTest(params string[] initialStrings)
        { 
            //为字符串分配内存空间
            elements = new String[8];
            //复制传递给构造方法的字符串
            foreach (string s in initialStrings)
            {
                elements[ctr++] = s; 
            }
        }

        /** <summary>
        ///  构造函数
        /// </summary>
        /// <param name="source">初始化的字符串</param>
        /// <param name="delimiters">分隔符,可以是一个或多个字符分隔</param>
        ForeachTest(string initialStrings, char[] delimiters) 
        {
            elements = initialStrings.Split(delimiters);
        }

        //实现接口中得方法
        public IEnumerator GetEnumerator()
        {
            return  new ForeachTestEnumerator(this);
        }

        private class ForeachTestEnumerator : IEnumerator
        {
            private int position = -1;
            private ForeachTest t;
            public ForeachTestEnumerator(ForeachTest t)
            {
                this.t = t;
            }

            实现接口#region 实现接口

            public object Current
            {
                get
                {
                    return t.elements[position];
                }
            }

            public bool MoveNext()
            {
                if (position < t.elements.Length - 1)
                {
                    position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

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

            #endregion
        }
        static void Main(string[] args)
        {
            // ForeachTest f = new ForeachTest("This is a sample sentence.", new char[] { ' ', '-' });
            ForeachTest f = new ForeachTest("This", "is", "a", "sample", "sentence.");
            foreach (string item in f)
            {
                System.Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}



转载地址:  http://www.studyofnet.com/news/501.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值