I have a class that map objects to objects, but unlike dictionary it maps them both ways. I am now trying to implement a custom IEnumerator
interface that iterates through the values.
public class Mapper<K,T> : IEnumerable<T>, IEnumerator<T>
{
C5.TreeDictionary<K,T> KToTMap = new TreeDictionary<K,T>();
C5.HashDictionary<T,K> TToKMap = new HashDictionary<T,K>();
public void Add(K key, T value)
{
KToTMap.Add(key, value);
TToKMap.Add(value, key);
}
public int Count
{
get { return KToTMap.Count; }
}
public K this[T obj]
{
get
{
return TToKMap[obj];
}
}
public T this[K obj]
{
get
{
return KToTMap[obj];
}
}
public IEnumerator<T> GetEnumerator()
{
return KToTMap.Values.GetEnumerator();
}
public T Current
{
get { throw new NotImplementedException(); }
}
public void Dispose()
{
throw new NotImplementedException();
}
object System.Collections.IEnumerator.Current
{
get { throw new NotImplementedException(); }
}
public bool MoveNext()
{
;
}
public void Reset()
{
throw new NotImplementedException();
}
}
IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. This works for readonly access to a collection that implements that IEnumerable can be used with a foreach statement. IEnumerator has two methods MoveNext and Reset. It also has a property called Current.
An object implementing IEnumerable allows others to visit each of its items (by an enumerator). An object implementing IEnumerator is the doing the iteration. It's looping over an enumerable object.
The IEnumerable interface provides support for the foreach iteration.
Implementing IEnumerator and IEnumerable Interfaces using Inner Class in C# 1 Create a file ItemCollection.cs and save the source in that. ... 2 Compile the above code as csc ItemCollection.cs 3 Run the file ItemCollection.exe to see following output in console window :
Within ItemCollection class there is one more class ( inner class or Nested Type ) called ItemIterator which implements IEnumerator interface. ItemCollection class also contains a string array itemId, which provides the basis for iteration.
IEnumerator is the base interface for all non-generic enumerators. Its generic equivalent is the System.Collections.Generic.IEnumerator<T> interface. The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection. The Reset method is provided for COM interoperability and does not need to be fully implemented; instead, the implementer can throw a NotSupportedException.
Just implement the IEnumerable<T>
interface. No need to implement the IEnumerator<T>
unless you want to do some special things in the enumerator, which for your case doesn't seem to be needed.
public class Mapper<K,T> : IEnumerable<T> {
public IEnumerator<T> GetEnumerator()
{
return KToTMap.Values.GetEnumerator();
}
}
and that's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With