码迷,mamicode.com
首页 > Windows程序 > 详细

C#自己写的迭代器(拓展字典)

时间:2015-09-09 00:54:39      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace IEnumerableIEnumerator3
{
    struct MyKeyValue<TKey,TValue1,TValue2,TValue3>
    {
        public TKey key;
        public TValue1 value1;
        public TValue2 value2;
        public TValue3 value3;
        public MyKeyValue(TKey key, TValue1 value1, TValue2 value2, TValue3 value3)
        {
            this.key = key;
            this.value1 = value1;
            this.value2 = value2;
            this.value3 = value3;
        }
    }
    class MyIEnumerable<TKey, TValue1, TValue2, TValue3> : IEnumerable<MyKeyValue<TKey, TValue1,TValue2,TValue3>>
    {
        private List<MyKeyValue<TKey, TValue1, TValue2, TValue3>> list;
        public MyIEnumerable()
        {
            list = new List<MyKeyValue<TKey, TValue1, TValue2, TValue3>>();
        }
        public void Add(TKey key, TValue1 value1, TValue2 value2, TValue3 value3)
        {
            MyKeyValue<TKey, TValue1, TValue2, TValue3> pair = new MyKeyValue<TKey, TValue1, TValue2, TValue3>(key, value1, value2, value3);
            list.Add(pair);
        }
        public IEnumerator<MyKeyValue<TKey, TValue1, TValue2, TValue3>> GetEnumerator()
        {
            for (int index = 0; index < list.Count; index++)
            {
                yield return list[index];
            }
            Console.WriteLine("泛型迭代完毕");
            //结束迭代
            yield break;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyIEnumerable<string, int, int, int> dayDic = new MyIEnumerable<string, int, int, int>();
            dayDic.Add("Mon",1,1,1);
            dayDic.Add("Tue",2,2,2);
            dayDic.Add("Wed",3,3,3);
            dayDic.Add("4Thu",4,4,4);
            dayDic.Add("Fri",5,5,5);
            dayDic.Add("Sat",6,6,6);
            dayDic.Add("Sun",7,7,7);

            foreach (var day in dayDic)
            {
                Console.Write(day.key + "  ");
                Console.Write(day.value1 + "  ");
                Console.Write(day.value2 + "  ");
                Console.Write(day.value3);
                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

C#自己写的迭代器(拓展字典)

标签:

原文地址:http://www.cnblogs.com/baoluqi/p/4793405.html

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