<p>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 <code>IEnumerator</code> interface that iterates through the values.</p>
<pre><code>public class Mapper&lt;K,T&gt; : IEnumerable&lt;T&gt;, IEnumerator&lt;T&gt;

{
    C5.TreeDictionary&lt;K,T&gt; KToTMap = new TreeDictionary&lt;K,T&gt;();
    C5.HashDictionary&lt;T,K&gt; TToKMap = new HashDictionary&lt;T,K&gt;();

    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&lt;T&gt; 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();
    }
}
</code></pre>

[{'answer_id': 53986, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 2, 'selected': False, 'text': '<p>Use yield return.</p>\n\n<p><a href="https://stackoverflow.com/questions/39476/what-is-the-yield-keyword-used-for-in-c#39496">What is the yield keyword used for in C#?</a></p>\n'}, {'answer_id': 53999, 'author': 'Pop Catalin', 'author_id': 4685, 'author_profile': 'https://Stackoverflow.com/users/4685', 'pm_score': 3, 'selected': False, 'text': "<p>Just implement the <code>IEnumerable&lt;T&gt;</code> interface. No need to implement the <code>IEnumerator&lt;T&gt;</code> unless you want to do some special things in the enumerator, which for your case doesn't seem to be needed.</p>\n<pre><code>public class Mapper&lt;K,T&gt; : IEnumerable&lt;T&gt; {\n    public IEnumerator&lt;T&gt; GetEnumerator()\n    {\n        return KToTMap.Values.GetEnumerator();\n    }\n}\n</code></pre>\n<p>and that's it.</p>\n"}, {'answer_id': 54022, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 5, 'selected': True, 'text': '<p>First, don\'t make your collection object implement IEnumerator&lt;>.  This leads to bugs.  (Consider the situation where two threads are iterating over the same collection).  </p>\n\n<p>Implementing an enumerator correctly turns out to be non-trivial, so C# 2.0 added special language support for doing it, based on the \'yield return\' statement.</p>\n\n<p>Raymond Chen\'s recent series of blog posts ("The implementation of iterators in C# and its consequences") is a good place to get up to speed.  </p>\n\n<ul>\n<li>Part 1: <a href="https://web.archive.org/web/20081216071723/http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx" rel="nofollow noreferrer">https://web.archive.org/web/20081216071723/http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx</a></li>\n<li>Part 2: <a href="https://web.archive.org/web/20080907004812/http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx" rel="nofollow noreferrer">https://web.archive.org/web/20080907004812/http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx</a></li>\n<li>Part 3: <a href="https://web.archive.org/web/20080824210655/http://blogs.msdn.com/oldnewthing/archive/2008/08/14/8862242.aspx" rel="nofollow noreferrer">https://web.archive.org/web/20080824210655/http://blogs.msdn.com/oldnewthing/archive/2008/08/14/8862242.aspx</a></li>\n<li>Part 4: <a href="https://web.archive.org/web/20090207130506/http://blogs.msdn.com/oldnewthing/archive/2008/08/15/8868267.aspx" rel="nofollow noreferrer">https://web.archive.org/web/20090207130506/http://blogs.msdn.com/oldnewthing/archive/2008/08/15/8868267.aspx</a></li>\n</ul>\n'}, {'answer_id': 14999790, 'author': 'Jack', 'author_id': 794594, 'author_profile': 'https://Stackoverflow.com/users/794594', 'pm_score': 3, 'selected': False, 'text': '<p><code>CreateEnumerable()</code> returns an <code>IEnumerable</code> which implements <code>GetEnumerator()</code></p>\n\n<pre><code>public class EasyEnumerable : IEnumerable&lt;int&gt; {\n\n    IEnumerable&lt;int&gt; CreateEnumerable() {\n        yield return 123;\n        yield return 456;\n        for (int i = 0; i &lt; 6; i++) {\n            yield return i;\n        }//for\n    }//method\n\n    public IEnumerator&lt;int&gt; GetEnumerator() {\n        return CreateEnumerable().GetEnumerator();\n    }//method\n\n    IEnumerator IEnumerable.GetEnumerator() {\n        return CreateEnumerable().GetEnumerator();\n    }//method\n\n}//class\n</code></pre>\n'}, {'answer_id': 23442029, 'author': 'Rezo Megrelidze', 'author_id': 2204040, 'author_profile': 'https://Stackoverflow.com/users/2204040', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s an example from the book "Algorithms (4th Edition) by Robert Sedgewick".</p>\n\n<p>It was written in java and i basically rewrote it in C#.</p>\n\n<pre><code>public class Stack&lt;T&gt; : IEnumerable&lt;T&gt;\n{\n    private T[] array;\n\n    public Stack(int n)\n    {\n        array = new T[n];\n    }\n\n    public Stack()\n    {\n        array = new T[16];\n    }\n\n    public void Push(T item)\n    {\n        if (Count == array.Length)\n        {\n            Grow(array.Length * 2);\n        }\n\n        array[Count++] = item;\n    }\n\n    public T Pop()\n    {\n        if (Count == array.Length/4)\n        {\n            Shrink(array.Length/2);\n        }\n\n        return array[--Count];\n    }\n\n    private void Grow(int size)\n    {\n        var temp = array;\n        array = new T[size];\n        Array.Copy(temp, array, temp.Length);\n    }\n\n    private void Shrink(int size)\n    {\n        Array temp = array;\n        array = new T[size];\n        Array.Copy(temp,0,array,0,size);\n    }\n\n    public int Count { get; private set; }\n    public IEnumerator&lt;T&gt; GetEnumerator()\n    {\n        return new ReverseArrayIterator(Count,array);\n    }\n\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return GetEnumerator();\n    }\n\n\n    // IEnumerator implementation\n    private class ReverseArrayIterator : IEnumerator&lt;T&gt;\n    {\n        private int i;\n\n        private readonly T[] array;\n\n        public ReverseArrayIterator(int count,T[] array)\n        {\n            i = count;\n            this.array = array;\n        }\n\n        public void Dispose()\n        {\n\n        }\n\n        public bool MoveNext()\n        {\n            return i &gt; 0;\n        }\n\n        public void Reset()\n        {\n\n        }\n\n        public T Current { get { return array[--i]; } }\n\n        object IEnumerator.Current\n        {\n            get { return Current; }\n        }\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/53967', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4694/']