【问题标题】:implement IEnumerator and IEnumerable in ICollection在 ICollection 中实现 IEnumerator 和 IEnumerable
【发布时间】:2013-10-18 04:33:47
【问题描述】:

我对这个 ICollection 东西很陌生,需要一些关于如何实现 IEnumerable 和 IEnumerator 的指导。我检查了 Microsoft 文档,我想我理解那里所说的(我认为)。但是当我尝试在我的案例中实现它时,我有点困惑,可能需要澄清一下。

基本上,我声明了一个类 T,然后是另一个实现 ICollection 的类 T。在 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}};
        .........

【问题讨论】:

  • 为什么要实现自己的 ICollection?如果您想添加自定义功能,为什么不使用现有的内置集合之一(如 List)甚至从它们派生?
  • 泛型请...现在是 2013 年 :)

标签: c# icollection


【解决方案1】:

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

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

对于通用版本,请在您的收藏中致电GetEnumerator

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

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

【讨论】:

    【解决方案2】:

    老实说,您不需要围绕字典构建自己的集合“包装器”,但如果必须,您可以将几乎所有调用委托给字典以实现 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);
            }
        }
    

    【讨论】:

    • 谢谢帕特尔。它确实有帮助。我喜欢你的答案和西蒙,但只能将一个标记为答案。我希望我可以将两者都标记为答案。每个都有自己的观点。谢谢大家!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 2010-11-12
    相关资源
    最近更新 更多