-2

I have a class with an indexer implemented. I would like to be able to run a foreach loop on the this but it says that I have to implement the Enumerator interface. Which makes me implement two methods:

    #region IEnumerable implementation

    public IEnumerator<Type> GetEnumerator() {
        throw new NotImplementedException();
    }

    #endregion

    #region IEnumerable implementation

    IEnumerator IEnumerable.GetEnumerator() {
        throw new NotImplementedException();
    }

    #endregion
  1. Could you please example me what's the difference between these two?
  2. What kind of implementation it requires?

The class is built this way:

  1. It has a field which is an array to keep the data.
  2. It has an indexer to get and set specific elements in the above mentioned array.

I want to be able to run a foreach loop on the this within the class, so I can get and set the array field using the indexer's get and set logic.

That's why I'm required to implement the GetEnumerator methods, but I have no clue what kind of implementation is required there.

Thanks for you helping.

Note: The How do I implement IEnumerable<T> thread did not help me, I've read it. The only implementation there is to return the array's enumerator.

Community
  • 1
  • 1
johni
  • 5,342
  • 6
  • 42
  • 70
  • Why are you not just iterating that internal array, instead of `this`? – Servy Jun 26 '15 at 18:19
  • Because there's some logic behind it. I should return the internal array cell's value only in a specific case. Normally I should return a value that is saved in another field. – johni Jun 26 '15 at 18:20
  • possible duplicate of [How do I implement IEnumerable](http://stackoverflow.com/questions/11296810/how-do-i-implement-ienumerablet) – Matías Fidemraizer Jun 26 '15 at 18:21
  • It didn't help me, as there's no specific implementation there. It's just returning the array's Enumerator. – johni Jun 26 '15 at 18:23
  • Put your implementation in `public IEnumerator GetEnumerator()`, then have the other just `return GetEnumerator();` – willaien Jun 26 '15 at 18:25
  • I failed to understand the question. Can you post short and complete code to demonstrate the issue. This will be closed soon. Reopen when you have done that. – usr Jun 26 '15 at 18:30

1 Answers1

9

Here's a very simple example of implementing IEnumerable:

public class SomeEnumerable : IEnumerable<string>
{
    private string[] values = new string[] { "foo", "bar" };

    #region Enumerator

    private IEnumerable<string> GetValues()
    {
        foreach(var s in values)
        {
            yield return s;
        }
    }

    #endregion Enumerator

    #region IEnumerable implementation

    public IEnumerator<string> GetEnumerator() {
        return GetValues().GetEnumerator();
    }

    #endregion

    #region IEnumerable implementation

    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }

    #endregion
}

Just replace the logic inside of GetValues() with whatever you need to, if you're doing something unusual.

willaien
  • 2,647
  • 15
  • 24
  • 3
    The `GetValues` method is unnecessary. Its body can be placed directly within the `GetEnumerator` method, since C# allows `yield return` to appear in methods returning `IEnumerator` (as well as methods that return `IEnumerable`). – Kyle Jun 26 '15 at 19:41