开发者

Rework custom collection to use whatever object

开发者 https://www.devze.com 2023-04-05 07:31 出处:网络
I have some code I always use to make collections of my object. This code and be used like 10 times in some of my project with the object replaced.

I have some code I always use to make collections of my object. This code and be used like 10 times in some of my project with the object replaced.

Is there any way I can create this as a function that can take whatever object? I've heard of <T> but I am unsure how to use it in this.

public class PageCollection : CollectionBase
    {
        public void Add(Page item)
        {
            InnerList.Add(item);
        }

        public void Remove(int index)
  开发者_高级运维      {
            InnerList.RemoveAt(index);
        }

        public void Remove(Page item)
        {
            InnerList.Remove(item);
        }

        public Page this[int index]
        {
            get
            {
                return (Page)InnerList[index];
            }
            set
            {
                InnerList[index] = value;
            }
        }
    }


Just use List<T> object to create strongly typed collection of your objects. In your example use just

List<Page> listOfPages = new List<Page>()

Also check System.Collections.Generic namespace for strongly typed (generic) collections.


Best go to the source:

http://msdn.microsoft.com/en-us/library/512aeb7t.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消