• IEnumerable IEnumerator 分类: .NET 2014-05-13 17:38 350人阅读 评论(0) 收藏


    看了官方的、网上其他的,写了2个demo,只不过还是没发现哪里会用到。


    IEnumerable和IEnumerator都是System.Collections下的接口,结构分别如下:

    namespace System.Collections
    {
        public interface IEnumerable
        {
            IEnumerator GetEnumerator();
        }
    }

    namespace System.Collections
    {
        // Summary:
        //     Supports a simple iteration over a nongeneric collection.
        [ComVisible(true)]
        [Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
        public interface IEnumerator
        {
            // Summary:
            //     Gets the current element in the collection.
            //
            // Returns:
            //     The current element in the collection.
            //
            // Exceptions:
            //   System.InvalidOperationException:
            //     The enumerator is positioned before the first element of the collection or
            //     after the last element.
            object Current { get; }
    
            // Summary:
            //     Advances the enumerator to the next element of the collection.
            //
            // Returns:
            //     true if the enumerator was successfully advanced to the next element; false
            //     if the enumerator has passed the end of the collection.
            //
            // Exceptions:
            //   System.InvalidOperationException:
            //     The collection was modified after the enumerator was created.
            bool MoveNext();
            //
            // Summary:
            //     Sets the enumerator to its initial position, which is before the first element
            //     in the collection.
            //
            // Exceptions:
            //   System.InvalidOperationException:
            //     The collection was modified after the enumerator was created.
            void Reset();
        }
    }


    Demo1:

    class Program
    {
        static void Main(string[] args)
        {
            Book[] books = new Book[3] { new Book() { BookName = "java" }, new Book() { BookName = ".net" }, new Book() { BookName = "php" } };
    
            BookShop bookShop = new BookShop(books);
    
            //输出java .net php
            foreach (Book book in bookShop)
            {
                Console.WriteLine(book.BookName);
            }
            Console.Read();
        }
    }
    
    //此类没有实现IEnumerable,只实现GetEnumerator()方法即可,因为数组本身已经有鸟~
    public class BookShop
    {
        public Book[] books = new Book[4];
    
        public BookShop(Book[] books)
        {
            this.books = books;
        }
    
        public IEnumerator GetEnumerator()
        {
            return books.GetEnumerator();
        }
    }
    
    public class Book
    {
        public string BookName { get; set; }
    }


    Demo2:
    class Program
    {
        static void Main(string[] args)
        {
            StringArray strArray = new StringArray("java,.net,php");
    
            //输出java .net php
            foreach (string s in strArray)
            {
                Console.WriteLine(s);
            }
    
            Console.Read();
        }
    }
    
    public class StringArray
    {
        public string[] strArray;
    
        public StringArray(string str)
        {
            strArray = str.Split(',');
        }
    
        public IEnumerator GetEnumerator()
        {
            return new StringArrayEnumerator(strArray);
        }
    }
    
    public class StringArrayEnumerator : IEnumerator
    {
        int position = -1;
        string[] strArray;
    
        public StringArrayEnumerator(string[] strArray)
        {
            this.strArray = strArray;
        }
    
        public bool MoveNext()
        {
            if (position < this.strArray.Length - 1)
            {
                position++;
                return true;
            }
            else
            {
                return false;
            }
        }
    
        public object Current
        {
            get { return strArray.ElementAt(position); }
        }
    
        public void Reset()
        {
            this.position = -1;//回到初始值
        }
    }




    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    第19章 辅助器方法
    第18 章 视图
    第16章 过滤器
    第15章 控制器和动作
    第14章 高级路由特性
    第13章 URL 路由
    第12章 MVC项目综述
    第8章 SportsStorePeta 导航
    第9章 SportsStorePeta 完成购物车
    第10章 SportsStorePeta 管理
  • 原文地址:https://www.cnblogs.com/configman/p/4657545.html
Copyright © 2020-2023  润新知