C# 迭代器的使用例子

2020-12-13 15:18

阅读:509

标签:style   blog   http   io   color   ar   os   使用   for   

一、使用数组做迭代,foreach (Person p in people.Persons),如下:

soscw.com,搜素材soscw.com,搜素材
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IEnumerableTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Person[] persons = new Person[4]
            {
                new Person("John","Smith", 18, true), 
                new Person("Kite", "Green", 16, false),
                new Person("Lei", "Li", 17, true),
                new Person("Meimei", "Han", 15, false)
            };
            People people = new People(persons);
            foreach (Person p in people.Persons)
            {
                Console.WriteLine(p.ToString());
            }
        }
    }

    public class People
    {
        private Person[] _persons;
        public Person[] Persons
        {
            get { return _persons; }
            set { _persons = value; }
        }

        public People(Person[] persons)
        {
            this.Persons = persons;
        }
    }

    public class Person
    {
        public Person(string firstName, string lastName, int age, bool gender)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
            this.Gender = gender;
        }

        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        private bool _gender;
        public bool Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        public override string ToString()
        {
            string gender = string.Empty;
            if (this.Gender)
                gender = "male";
            else
                gender = "female";
            return string.Format("{0} {1}, {2}, is {3} years old.", this.FirstName, this.LastName, gender, this.Age);
        }
    }
}
View Code

soscw.com,搜素材

二、由于一些必要的原因,不希望在类的外面直接操作people.Persons,则对类People改造如下:

soscw.com,搜素材soscw.com,搜素材
    public class People
    {
        private Person[] _persons;
        public People(Person[] persons)
        {
            this._persons = new Person[persons.Length];
            for (int i = 0; i )
            {
                _persons[i] = persons[i];
            }
        }
    }
View Code

此时,对people.Persons进行遍历是不可能的。可是,我们仍然有对Persons进行遍历的需求。

怎么办?继续改造类People,让我们可以直接对类People进行遍历:foreach (Person p in people)

怎么改造?添加迭代器。

三、添加迭代器

1.继承IEnumerable

2.添加方法

IEnumerator IEnumerable.GetEnumerator()
    {
       ...
    }

如下:

soscw.com,搜素材soscw.com,搜素材
    public class People : IEnumerable
    {
        private Person[] _persons;
        public People(Person[] persons)
        {
            this._persons = new Person[persons.Length];
            for (int i = 0; i )
            {
                _persons[i] = persons[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            //...
        }
    }
View Code

 

C# 迭代器的使用例子

标签:style   blog   http   io   color   ar   os   使用   for   

原文地址:http://www.cnblogs.com/jmllc/p/4074010.html


评论


亲,登录后才可以留言!