在ICollection中实现IEnumerator和IEnumerable

时间:2013-10-18 04:33:48

标签: c# icollection

我对这个ICollection的东西很新,需要一些如何实现IEnumerable和IEnumerator的指导。我检查了Microsoft文档,我想我明白那里说的是什么(我认为)。但是当我试图在我的情况下实现它时,我有点困惑,可能需要一些澄清。

基本上,我宣布了一个T类,然后是另一个实现ICollection的类Ts。在Ts,我有一本字典。

从主程序中,我想像这样初始化类Ts: Ts ts = new Ts(){{a,b},{c,d}};

所以,我的问题是:

1)这样做是否合法?虽然我没有运行测试因为我还没有彻底实现IEnumerable和IEnumerator,这似乎是编译器没有投诉,这带来了我的第二个问题

2)如何实现IEnumerable和IEnumerator?

下面是我的伪代码来说明我的观点。

        public class T
        {
           string itemName;
           int    quantity;
           .....
           public T(string s, int q)
           { 
              .....
           }
        }
        public class Ts: ICollection
        {
            private Dictionary<string, T> inventory= new Dictionary<string,T>();

            public void Add(string s, int q)
            {
                inventory.Add(s, new T(s,q));
            }

             public IEnumerator<T> GetEnumerator()
            { 
            // please help              
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
            // what is the proper GetEnumerator here
            }
            ...
            implement other method in ICollection
        } 

        extract from the main program
        public Ts CollectionOfT = new Ts(){{"bicycle",100},{"Lawn mower",50}};
        .........

2 个答案:

答案 0 :(得分:4)

正确的实现是在显式实现中将您的集合转换为IEnumerable

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

对于通用版本,请在您的收藏集中调用GetEnumerator

public IEnumerator<T> GetEnumerator() {
    return your_collection_here.GetEnumerator();
}

您必须拥有支持自定义集合的内容,例如ListArray等。在这些实现中使用它。

答案 1 :(得分:1)

老实说,您不需要在Dictionary周围构建自己的集合“wrapper”,但如果必须,您可以将几乎所有的调用委托给字典来实现ICollection接口。 希望这有帮助

        public class Ts: ICollection<T>
    {
        private Dictionary<string, T> inventory= new Dictionary<string,T>();

        //public void Add(string s, int q)
        //{
        //    inventory.Add(s, new T(s,q));
        //} 
        public void Add(T item)
        {
            inventory.Add(item.ItemName,item);
        }
        public void Add(string s, int q)
        {
            inventory.Add(s, new T(s, q));
        }

        public void Clear()
        {
            inventory.Clear();
        }

        public bool Contains(T item)
        {
            return inventory.ContainsValue(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            inventory.Values.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return inventory.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            return inventory.Remove(item.ItemName);
        }

        public System.Collections.Generic.IEnumerator<T> GetEnumerator()
        {
            return inventory.Values.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return inventory.Values.GetEnumerator();
        }
    } 
   class Program
   {
        Ts ts = new Ts { { "a", 1 }, { "b", 2 } };
        foreach (T t in ts)
        {
            Console.WriteLine("{0}:{1}",t.ItemName,t.Quantity);
        }
    }