IEnumerable IEnumerator

using System;
using System.Collections.Generic;
using System.Collections;

namespace WroxBooks.chap05
{
	public class MainApp
	{
		public static void Main()
		{
			Person[] people = new Person[4];
			
			people[0] = new Person("scott");
			people[1] = new Person("tiger");
			people[2] = new Person("root");
			people[3] = new Person("white");
			
			People peoples = new People(people);
			
			foreach(Person p in peoples)
			{
				Console.WriteLine(p.Name);
			}
			
			Console.ReadKey();
			
		}
		
		public void Test()
		{
	
			var list = new List<string>();
			list.Add("string");
			
			
			
			
		}
	}
	
	class GenericClass<T>
	{
	}
	

	class Person
	{
		private string _name;
		
		public Person(string name)
		{
			_name = name;
		}
		
		public string Name
		{
			get{return _name;}
			set{_name = value;}
		}
	}
	
	class People : IEnumerable
	{
		private Person[] _people;
		
		public People(Person[] list)
		{
			_people = list;
		}
		
		IEnumerator IEnumerable.GetEnumerator()
		{
			return new PeopleEnumerator(_people);
		}
	}
	
	class PeopleEnumerator : IEnumerator
	{
		private Person[] _people;
		
		private int pos;
		
		public PeopleEnumerator(Person[] people)
		{
			_people = people;
			pos = -1;
		}
		
		
		public object Current 
		{
			get
			{
				if(pos > -1&&pos<_people.Length)
					return _people[pos];
				return null;
			}
		}
		
		public bool MoveNext()
		{
			pos++;
			return pos < _people.Length;
		}
		
		public void Reset()
		{
			pos = -1;
		}
	}
	
}


你可能感兴趣的:(IEnumerable IEnumerator)