zoukankan      html  css  js  c++  java
  • C#自己写的迭代器(拓展字典)

    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();
            }
        }
    }

  • 相关阅读:
    编写你自己的单点登录(SSO)服务
    SignalR + KnockoutJS + ASP.NET MVC4 实现井字游戏
    Quartz使用总结
    Quartz.Net入门
    ASP.NET批量下载文件的方法
    Asp.Net文件的上传和下载
    AOP实践—ASP.NET MVC5 使用Filter过滤Action参数防止sql注入,让你代码安全简洁
    ASP.NET MVC4中的异步控制器
    Mybatis表关联一对多、多对一、多对多
    Xamarin android使用Sqlite做本地存储数据库
  • 原文地址:https://www.cnblogs.com/baoluqi/p/4793405.html
Copyright © 2011-2022 走看看