赞
踩
using System; using System.Collections.Generic; public class RepeatDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> { private readonly List<TKey> _keys = new List<TKey>(); private readonly List<TValue> _values = new List<TValue>(); public void Add(TKey key, TValue value) { _keys.Add(key); _values.Add(value); } IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() { for (int i = 0; i < _keys.Count; i++) { yield return new KeyValuePair<TKey, TValue>(_keys[i], _values[i]); } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { for (int i = 0; i < _keys.Count; i++) { yield return new KeyValuePair<TKey, TValue>(_keys[i], _values[i]); } } } class Program { static void Main() { RepeatDictionary<string, string> dictionary = new RepeatDictionary<string, string>() { {"aa","bb" }, {"aa","cc" }, {"aa","dd" }, }; foreach (var item in dictionary) { Console.WriteLine($"{item.Key},{item.Value}"); } Console.ReadKey(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。