Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you implement the IEnumerator interface?

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();
    }
}
like image 236
Tomas Pajonk Avatar asked Sep 10 '08 13:09

Tomas Pajonk


People also ask

What is IEnumerator interface?

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.

What does an IEnumerator do?

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.

Which interface a type must have implemented to be integrated in foreach loop?

The IEnumerable interface provides support for the foreach iteration.

How to implement IEnumerator and IEnumerable interfaces using inner class in C #?

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 :

What is the difference between itemcollection and IEnumerator interface?

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.

What is the base interface for all non generic enumerators?

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.

What is the use of enumerator in COM?

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.


1 Answers

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.

like image 123
Pop Catalin Avatar answered Sep 18 '22 08:09

Pop Catalin