且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实现IEnumerable< T>对于包装清单

更新时间:2021-07-29 21:01:24

如果您实现 的ICollection&LT; INT&GT; 你所期望的功能。

If you implement ICollection<int> you get the desired functionality.

更正:你其实只需要执行的IEnumerable 的IEnumerable&LT; T&GT; ,并在你的类中的公共添加方法:

Correction: you actually only need to implement IEnumerable or IEnumerable<T> and have a public Add method in your class:

public class Wrapper : IEnumerable<int>
{
    public List<int> TList
    { get; private set; }
    public Wrapper()
    {
        TList = new List<int>();
    }

    public void Add(int item)
    {
        TList.Add(item);
    }
    public IEnumerator<int> GetEnumerator()
    {
        return TList.GetEnumerator();
    }

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



(我也参加了制作的***的的TList 设定部私人;它通常建议集合类型属性是只读使得集合因此不能由类型之外的任何代码被取代)

(I also took the liberty of making the TList setter private; it is usually recommended that collection type properties are read-only so that the collection as such can not be substituted by any code outside the type.)