第五章 数组(二)

Array 实现了IEumerable,ICollection,IList 接口

1.IEumerable

 foreach语句中使用枚举,可以迭代集合中的元素。

 数组或集合执行带GetEumerator()方法的IEumerable接口。

 GetEumerator()方法返回一个执行IEumerable接口的枚举。

 foreach 语句并不真的需要在集合类中执行接口,有一个名为GetEumerator()的方法,返回实现了IEnumerator接口的对象就足够了。

yield语句 便于创建枚举器

ex : public class HelloCollection

      {

         public IEumeraror GetEumerator()

           {

              yield return "hello";

              yield return "hello"; 

             }

        }

      

        public class Program

         {

            Hellocollection helloCollection=new Hellocellection();

            foreach(string s in helloCollection)

               {

                   Console.WriteLine(s);

                 }

          }

 yield return 还可以返回枚举器

2. ICollection

  ICollection接口派生于IEumerable 接口,主要确定元素个数,或用于同步

3.IList 

   IList接口派生于ICollection  接口。 IList最主要定义了Item属性,以使用索引器访问元素。

posted on 2010-12-30 10:49  小齐宝  阅读(145)  评论(0)    收藏  举报

导航