我有多个复数的项目类,每个都有单个项目类的集合,如下所示:

public class Contracts : Items
{
        public List<Contract> _collection = new List<Contract>();
        public List<Contract> Collection
        {
            get
            {
                return _collection;
            }
        }
}

public class Customers: Items
{
        public List<Customer> _collection = new List<Customer>();
        public List<Customer> Collection
        {
            get
            {
                return _collection;
            }
        }
}

public class Employees: Items
{
        public List<Employee> _collection = new List<Employee>();
        public List<Employee> Collection
        {
            get
            {
                return _collection;
            }
        }
}


我可以想象我可以使用泛型将其放入父类中。我应该怎么做,我想看起来像这样:

伪代码:

public class Items
{
        public List<T> _collection = new List<T>();
        public List<T> Collection
        {
            get
            {
                return _collection;
            }
        }
}

最佳答案

这是完全正确的,除了您还希望在Items之后输入<T>

public class Items<T>
{
        public List<T> _collection = new List<T>();
        public List<T> Collection
        {
            get
            {
                return _collection;
            }
        }
}


实例化:

Items<Contract> contractItems = new Items<Contract>();

10-06 06:38