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