自定义通用集合 [英] Custom Generic Collection

查看:69
本文介绍了自定义通用集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharp_p332_CustomGenericCollection
{
    public class CarCollection<T> : IEnumerable<T>
    {
        private List<T> arCars = new List<T>();

        public T GetCar(int pos)
        {
            return arCars[pos];
        }

        public void AddCar(T c)
        {
            arCars.Add(c);
        }

        public void ClearCars()
        {
            arCars.Clear();
        }

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

        // IEnumerable<T> extends IEnumerable, therefore we need to implement both versions of GetEnumerator()
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return arCars.GetEnumerator();
        }
    }
}




这是课程

CarCollection.cs

如果有人可以告诉我它有什么问题,那太好了..谢谢




This is the class

CarCollection.cs

If someone could tell me whats wrong with it , that would be great..thanks

推荐答案

要使用接口,您必须隐含其成员.您可以通过右键单击界面并告诉IDE为其自动实现模板来自动生成模板.

然后,您将获得属性和方法存根,如果被访问,它们将引发异常.然后,您将需要实际隐含该接口.

您可能要查看如何创建迭代器的MSDN通用列表的阻止 [ ^ ]
To use an interface you must impliment its members. You can auto generate the template by right clicking on the interface and telling the IDE to impliment it for you.

You will then get property and method stubs that will throw exceptions if accessed. You will then need to actually impliment the interface.

You may want to look over MSDN of How to create an iterator block for generic list[^]


好吧,您可以听Bob的建议,或者根据类定义的要求添加IEnumerable接口的实现.
Well you could take Bob''s advice, or add the implementation of the IEnumerable interface as required by your class definition.


如果集合是汽车的集合,为什么不只使用
If the collection is a collection of cars, why won''t you just use
public class CarCollection : IEnumerable


无论如何,IEnumerable要求您拥有IEnumerator
应该是这样的...我没有编译它,所以我不确定它实际上编译的很好,但是我认为它是正确的.


Anyway, IEnumerable requires that you would have an IEnumerator
It should be something like this... I didn''t compile it, so I''m not sure that it actually compiles good, but its about right I think.

public class CarCollection : IEnumerable
{

        List<car> cars;

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

        public CarEnum GetEnumerator()
        {
            return new CarEnum(cars);
        }

        public class CarEnum : IEnumerator
        {
            private List<car> cars;

            int position = -1;

            public CarEnum(List<car> commands)
            {
                this.cars = cars;
            }

            public bool MoveNext()
            {
                position++;
                return (position < cars.Count);
            }

            public void Reset()
            {
                position = -1;
            }

            object IEnumerator.Current
            {
                get
                {
                    return Current;
                }
            }

            public Car Current
            {
                get
                {
                    try
                    {
                        return cars[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }

        /* You can add more methods of course... */
}

</car></car></car>


这篇关于自定义通用集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆