C# - How to implement IEnumerator on a class

后端 未结 3 493
南旧
南旧 2020-12-18 22:29

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

public class Items
    {
        private Dictionary

        
相关标签:
3条回答
  • 2020-12-18 22:48

    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.

    0 讨论(0)
  • 2020-12-18 22:49

    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()
    
    0 讨论(0)
  • 2020-12-18 22:54

    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.

    0 讨论(0)
提交回复
热议问题