zoukankan      html  css  js  c++  java
  • 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;//回到初始值
        }
    }




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

  • 相关阅读:
    虚拟环境地址
    ubuntu 查看占用文件空间大小
    drf serializer官网所得
    百度api 找到当前电话号码归属地
    mongo 改bug
    django orm 读写分离,分库分app
    __setattr__,__getattr__,__getattribute__
    Python之路--Django--模型
    Python之路--Django--模板
    Python之路--Django--视图
  • 原文地址:https://www.cnblogs.com/configman/p/4657545.html
Copyright © 2011-2022 走看看