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

  • 相关阅读:
    【SQLServer】 查询一个字段里不同值的最新一条记录
    【MySQL】mysql5.7数据库的安装和配置
    【Java】JDK安装及环境变量配置
    【Oracle】SQL语句优化
    【JavaScript】 控制自适应高度
    【Java】登录验证码
    【JavaScript】 直接下载保存文件
    【Java】Java批量文件打包下载zip
    【Java】Java 单文件下载及重命名
    主机在无线网络的情况下,设置centos7.2虚拟机网络联通
  • 原文地址:https://www.cnblogs.com/baoluqi/p/4793405.html
Copyright © 2011-2022 走看看