• IEnumberable和IEnumberator理解


    1、  IEnumberable接口很简单就一个GetEnumberator()方法;它返回的是一IEnumberator对象这是一个可以循环访问集合的对象。IEnumberator是一个集合访问器,没有他就不能使用foreach语句遍历集合或数组,因为只有IEnumberator才能访问集合中的项,IEnumberator定义了Current属性,MoveNext和Reset两个方法;Current用来获取集合中的项,MoveNext方法只是将游标内部位置向前移动(就是移到下一个元素);

    2、  例子:

    public class Book : IEnumerable

        {

            public string Name;                                                                           

            public string Price;                                                                               

            public Book(string name, string price)                                                      

            {

                Name = name;                                                                              

                Price = price;                                                                                     

            }

            private Book[] book;

            public Book(Book[] array) //

            {

                book = array;

            }

            public IEnumerator GetEnumerator()    //实现接口

            {

                return this.book.GetEnumerator();  //返回方法,用于 System.Array 的 System.Collections.IEnumerator。System.Array类型和其他许多类型(如List)已经实现了IEnumerable和IEnumerator接口,你可以简单委托请求到System.Array.

            }

    }

    Main方法

    static void Main(string[] args)

            {

                Book[] book = new Book[3]        

                {

                    new Book("水浒","21"),           

                    new Book("三国","21"),  

                    new Book("西游","21")

                };

                foreach (Book p in book) 

                {

                    Console.WriteLine("书的名字:" + p.Name + ";价格: " + p.Price);

                }

                Console.WriteLine("用数组形式的输出");

                Book booklist = new Book(book);

                foreach (Book p in booklist)

                {

                    Console.WriteLine("书的名字:" + p.Name + ";价格: " + p.Price);

                }

            }

    上述代码就可以了,一个自定义的数组就可以使用foreach;

    3、  手工实现IEnumberable接口和Ienumberator

    class BookEnum : IEnumerator  //实现foreach语句内部,并派生

        {

            public Book[] _book; //实现数组

            int position = -1;//设置“指针”

            public BookEnum(Book[] list)

            {

                _book = list; //实现list

            }

            public bool MoveNext()//实现向前移动

            {

                position++;     //位置增加

                return (position < _book.Length);   //返回布尔值

            }

            public void Reset()     //位置重置

            {

                position = -1;

            }//重置指针为-1

            public object Current      //实现接口方法

            {

                get

                {

                    try

                    {

                        return _book[position];      //返回对象

                    }

                    catch (IndexOutOfRangeException)      //捕获异常

                    {

                        throw new InvalidOperationException();     //抛出异常信息

                    }

                }

            }

    }

    实现IEnumberable方法

            public IEnumerator GetEnumerator()    //实现接口

            {

                return new BookEnum (book);      //返回方法

            }

    4、  IEnumberable<T>接口

    他是一个强类型的,他为子对象的迭代提供了类型更加安全的方式;

  • 相关阅读:
    java线程
    windows Server 安装
    nginx正则反向代理
    crontab定时任务启动脚本失败
    数据结构
    异常概念
    shell日期遍历复制文件
    多态和抽象
    图1 列出连通集
    PTA 代码注意事项
  • 原文地址:https://www.cnblogs.com/heluo/p/2387488.html
Copyright © 2020-2023  润新知