zoukankan      html  css  js  c++  java
  • 自定义集合支持使用 foreach循环访问该集合

    自定义集合支持使用 foreach循环访问该集合,需要实现 IEnumerableIEnumerator 接口。

    using System;
    using System.Collections;

    public class Person
    {
       
    public Person(string fName, string lName)
       {
          
    this.firstName = fName;
          
    this.lastName = lName;
       }

       
    public string firstName;
       
    public string lastName;
    }

    public class People : IEnumerable
    {
       
    private Person[] _people;
       
    public People(Person[] pArray)
       {
          _people 
    = new Person[pArray.Length];

          
    for (int i = 0; i < pArray.Length; i++)
          {
             _people[i] 
    = pArray[i];
          }
       }

       
    public IEnumerator GetEnumerator()
       {
          
    return new PeopleEnum(_people);
       }
    }

    public class PeopleEnum : IEnumerator
    {
       
    public Person[] _people;

       
    // Enumerators are positioned before the first element
       
    // until the first MoveNext() call.
       int position = -1;

       
    public PeopleEnum(Person[] list)
       {
          _people 
    = list;
       }

       
    public bool MoveNext()
       {
          position
    ++;
          
    return (position < _people.Length);
       }

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

       
    public object Current
       {
          
    get
          {
             
    try
             {
                
    return _people[position];
             }
             
    catch (IndexOutOfRangeException)
             {
                
    throw new InvalidOperationException();
             }
          }
       }
    }

    class Example
    {
       
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
       {
          Person[] peopleArray 
    = new Person[3]
            {
                
    new Person("John""Smith"),
                
    new Person("Jim""Johnson"),
                
    new Person("Sue""Rabon"),
            };

          People peopleList 
    = new People(peopleArray);
          
    foreach (Person p in peopleList)
             outputBlock.Text 
    += p.firstName + " " + p.lastName + " ";

       }
    }

    /* This code produces output similar to the following:
     * 
     * John Smith
     * Jim Johnson
     * Sue Rabon
     * 
     
    */

    作者:freshman1987
    出处:http://www.cnblogs.comfreshman1987/
    本文版权归作者所有,欢迎转载,但未经作者同意不能转载,否则保留追究法律责任的权利。
  • 相关阅读:
    codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
    13年山东省赛 The number of steps(概率dp水题)
    13年山东省赛 Mountain Subsequences(dp)
    13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
    codeforces 337C Quiz(贪心)
    codeforces 336C Vasily the Bear and Sequence(贪心)
    codeforces 335A Banana(贪心)
    codeforces 339C Xenia and Bit Operations(线段树水题)
    codeforces 339C Xenia and Weights(dp或暴搜)
    codeforces 340E Iahub and Permutations(错排or容斥)
  • 原文地址:https://www.cnblogs.com/freshman1987/p/3747567.html
Copyright © 2011-2022 走看看