Rework custom collection to use whatever object
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
精彩评论