Реализация IEnumerable<> - C#
Формулировка задачи:
Всем привет, не понимаю как реализовать интерфейс IEnumerable<KeyValuePair<K, V>> в данном коде:
Объясните подробно если можно =)
public class MultiDictionary<K, V> : IMultiDictionary<K, V>, IEnumerable<KeyValuePair<K, V>>
{
Dictionary<K, LinkedList<V>> internalDictionary = new Dictionary<K, LinkedList<V>>();
public int Count
{
get
{
throw new NotImplementedException();
}
}
public ICollection<K> Keys
{
get
{
throw new NotImplementedException();
}
}
public ICollection<V> Values
{
get
{
throw new NotImplementedException();
}
}
public void Add(K key, V value)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(K key, V value)
{
throw new NotImplementedException();
}
public bool ContainsKey(K key)
{
throw new NotImplementedException();
}
public bool Remove(K key)
{
throw new NotImplementedException();
}
public bool Remove(K key, V value)
{
throw new NotImplementedException();
}
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
return internalDictionary.Values.GetEnumerator(); // Соответственно кидает ошибку, текст ниже
}
/*
Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.Dictionary<K, System.Collections.Generic.LinkedList<V>>.ValueCollection.Enumerator' to 'System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<K, V>>'
*/
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}Решение задачи: «Реализация IEnumerable<>»
textual
Листинг программы
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
foreach (var kvp in internalDictionary)
foreach (var value in kvp.Value)
yield return new KeyValuePair<K, V>(kvp.Key, value);
}