Riz
Riz

Reputation: 6982

C# - How to implement IEnumerator on a class

How to implement IEnumerator on this class so that I can use it in foreach loop.

public class Items
    {
        private Dictionary<string, Configuration> _items = new Dictionary<string, Configuration>();

        public Configuration this[string element]
        {
            get
            {

                if (_items.ContainsKey(element))
                {
                    return _items[element];
                }
                else
                {
                    return null;
                }
            }

            set
            {
                _items[element] = value;
            }
        }
   }

In this example Configuration is a simple class with few properties.

Upvotes: 14

Views: 26645

Answers (3)

Simon Fischer
Simon Fischer

Reputation: 3876

You should be able to implement IEnumerator like this:

public class Items : IEnumerator<KeyValuePair<string, Configuration>>  
{        
    private Dictionary<string, Configuration> _items = new Dictionary<string, Configuration>();        
    public Configuration this[string element]       
    {            
        get
        {
            if (_items.ContainsKey(element))
            {
                return _items[element];
            }
            else
            {
                return null;
            }
        }
        set
        {
             _items[element] = value;
        }
    }

    int current;
    public object Current
    {
        get { return _items.ElementAt(current); }
    }

    public bool MoveNext()
    {
        if (_items.Count == 0 || _items.Count <= current)
        {
            return false;
        }
        return true;
    }

    public void Reset()
    {
        current = 0;
    }

    public IEnumerator GetEnumerator()
    {
        return _items.GetEnumerator();
    }

    KeyValuePair<string, Configuration> IEnumerator<KeyValuePair<string, Configuration>>.Current
    {
        get { return _items.ElementAt(current); }
    }

    public void Dispose()
    {
        //Dispose here
    }
}

But as already noted you could also just implement IEnumerable.

Upvotes: 6

nakhli
nakhli

Reputation: 4059

You don't need to implement IEnumerable or any interface. In order to be able to use your class in a foreach, all that you need is to add an instance method to your class with the follwing signature:

IEnumerator GetEnumerator()

Upvotes: 5

Tigran
Tigran

Reputation: 62248

Just an example to implement typesafe IEnumerable and not IEnumerator which you will be able to use in foreach loop.

   public class Items : IEnumerable<Configuration>
    {
        private Dictionary<string, Configuration> _items = new Dictionary<string, Configuration>();


        public void Add(string element, Configuration config) {
            _items[element] = config;
        }

        public Configuration this[string element]
        {
            get
            {

                if (_items.ContainsKey(element))
                {
                    return _items[element];
                }
                else
                {
                    return null;
                }
            }

            set
            {
                _items[element] = value;
            }
        }

        public IEnumerator<Configuration> GetEnumerator()
        {
            return _items.Values.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _items.Values.GetEnumerator();
        }
    }

Regards.

Upvotes: 16

Related Questions