zoukankan      html  css  js  c++  java
  • 关于迭代器中IEnumerable与IEnumerator的区别

    首先是IEnumerable与IEnumerator的定义:

    1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项。

    2.IEnumerator接口是一个真正的集合访问器,它包含MoveNext()方法和Current属性,在foreach循环中,如果MoveNext()返回True,则就是用IEnumerator接口的Current属性来获取对象的一个引用,用于foreach循环。

    3.如果要迭代一个类,可以使用GetEnumerator(),其返回类型是IEnumerator.

     如果要迭代一个类成员,则用IEnumerable.

    下面的例子是迭代Person类中的类成员Ages,使用了IEnumerable。第二个例子则是迭代一个类,所以使用了IEnumerator作为返回值。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace _10_5_5
    {
        public class person
        {
            private string name;
            private int age;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    age = value;
                }
            }
            public person(string PName, int PAge)
            {
                Name = PName;
                Age = PAge;
            }
            public static bool operator >(person a, person b)
            {
                if (a.Age > b.Age)
                    return true;
                else
                    return false;
            }
            public static bool operator <(person a, person b)
            {
                if (a.Age > b.Age)
                    return false;
                else
                    return true;
            }
            public static bool operator >=(person a, person b)
            {
                if (a.Age >= b.Age)
                {
                    return true;
                }
                else
                    return false;
            }
            public static bool operator <=(person a, person b)
            {
                if (a.Age <= b.Age)
                    return true;
                else
                    return false;
            }
        }
        public class People : DictionaryBase
        {
            public IEnumerable Ages//注意是IEnumerable
            {
                get
                {
                    foreach (object person in Dictionary.Values)
                    {
                        yield return (person as person).Age;
                    }
                }
            }
            public person[] GetOldest()
            {
                People oldPeople = new People();
                person oldPerson = null;
                person currentPerson;
                foreach (DictionaryEntry myPeople in Dictionary)
                {
                    currentPerson = myPeople.Value as person;
                    if (oldPerson == null)
                    {
                        oldPerson = currentPerson;
                        oldPeople.Add(oldPerson);
                    }
                    else
                    {
                        if (currentPerson > oldPerson)
                        {
                            oldPeople.Clear();
                            oldPeople.Add(currentPerson);
                            oldPerson = currentPerson;
                        }
                        else
                        {
                            if (currentPerson >= oldPerson)
                            {
                                oldPeople.Add(currentPerson);
                            }
                        }
                    }
                }
                person[] oldestPeopleArray = new person[oldPeople.Count];
                int copyIndex = 0;
                foreach (DictionaryEntry p in oldPeople)
                {
                    oldestPeopleArray[copyIndex] = p.Value as person;
                    copyIndex++;
                }
                return oldestPeopleArray;
            }
            public void Add(person p)
            {
                Dictionary.Add(p.Name, p);
            }
            public person this[string SName]
            {
                get
                {
                    return (person)Dictionary[SName];
                }
                set
                {
                    Dictionary[SName] = value;
                }
            }
    
        }
        class Program
        {
            static void Main(string[] args)
            {
                person a = new person("Jack", 11);
                person b = new person("Json", 10);
                People s = new People();
                s.Add(a);
                s.Add(b);
                foreach(int age in s.Ages)
                {
                    Console.WriteLine("{0}	", age);
                }
                Console.ReadKey();
            }
        }
    }

    下面是自定义的一个迭代器的例子:

    Primer.CS

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Ch11Ex03_Exam
    {
        public class Primes
        {
            private long min;
            private long max;
            public Primes():this(2,100)
            {
                
            }
            public Primes(long minNum,long maxNum)
            {
                if(minNum<2)
                {
                    min=2;
                }else{
                    min = minNum;
                }
                max = maxNum;
            }
            public IEnumerator GetEnumerator()//返回的是IEnumerator
            {
                for(long i=min;i<max;i++)
                {
                    int flag = 1;
                    for(long j=2;j<Math.Sqrt(min);j++)
                    {
                        if(i%j==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(flag==1)
                    {
                        yield return i;
                    }
                }
            }
        }
    }

    Program.CS:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Ch11Ex03_Exam
    {
        class Program
        {
            static void Main(string[] args)
            {
                Primes s = new Primes(2, 100);
                foreach(long i in s)
                {
                    Console.WriteLine("{0}	", i);
                }
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    So sad! ,Asphyre Closure
    Asphyre Sphinx is a cross-platform framework for developing 2D/3D video games and interactive business applications
    Mark: admob for delphi xe4 integrated 80% -done!-95% to do more test
    CCBPM新手流程设计教程
    CCBPM 常用API接口说明
    CCBPM H5版本中组织结构集成以及与外部数据源同步介绍
    关于驰骋工作流引擎ccbpm 在工业自动化环境下的应用演示实例
    关于驰骋工作流引擎ccbpm 在工业自动化环境下的 应用演示实例
    CCFlow新版本的自由流程、自定义流程功能说明
    关于驰骋工作流引擎ccbpm对bpmn2.0的支持
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/5372292.html
Copyright © 2011-2022 走看看