码迷,mamicode.com
首页 > 其他好文 > 详细

泛型迭代器

时间:2016-12-28 12:00:35      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:length   eset   cat   generic   stat   pac   var   数组   ati   

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strl = { "1","2","3","4"};
            People<string> s = new People<string>(strl);
            
            foreach (var item in s)
            {
                Console.WriteLine(item);
            }
        }
    }
    public class People<T> : IEnumerable
    {
        private T[] _People;
                               //List<T> _arryList
        public People(T[] _ArryList)
        {
            _People = new T[_ArryList.Length];
            for (int i = 0; i < _ArryList.Length; i++)
            {
                _People[i] = _ArryList[i];
            }
        }
        //实现接口方法
        public IEnumerator GetEnumerator()
        {
            return new EnumeratorItem<T>(_People);
        }
    }
             //这个泛型得到People泛类型、得到实例化
    public class EnumeratorItem<T> : IEnumerator
    {
        public T[] _PeopleList;
        int position = -1;
        public EnumeratorItem(T[] _itemList)
        {
            _PeopleList = new T[_itemList.Length];
            _PeopleList = _itemList;
        }
        public object Current
        {
            get
            {
                try
                {
                    //返回该当前数组项
                    return _PeopleList[position];
                }
                catch (Exception)
                {
                    
                    throw;
                };
            }
        }
        public bool MoveNext()
        {
            position++;
            return (position < _PeopleList.Length);
        }

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


}

泛型迭代器

标签:length   eset   cat   generic   stat   pac   var   数组   ati   

原文地址:http://www.cnblogs.com/1ddd/p/6228750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!