在C#中究竟意味着什么?

Fan*_*nda 0 .net c# generics

我是C#的新手,来自PHP.一切都很清楚,除了我不知道究竟是什么意思构造接口/集合<>.

我已经在使用<>作为"类型定义"了,但它究竟意味着什么.我这样使用它:

class TaskComparer : IEqualityComparer<Task> ...
class TaskQueue : ConcurrentQueue<Task> ...
Run Code Online (Sandbox Code Playgroud)

你看到有关于此的任何文章吗?请发布链接.谢谢.

McG*_*gle 5

它被称为"泛型",它是(如你所说)定义类型参数的一种方式.这是MSDN上的一篇介绍性文章:http: //msdn.microsoft.com/en-us/library/ms172192.aspx

作为实用程序的一个简单示例,请考虑一个Repository模式.

public interface IRepository<T> {
    public void Add (T obj);
    public IEnumerable<T> GetAll();
}
Run Code Online (Sandbox Code Playgroud)

通过正确的实现,现在您可以为任何类型的实体提供存储库接口.

public void someMethod(IRepository<Person> personsRepo) 
{
    IEnumerable<Person> persons = personsRepo.GetAll();
    // ...
}
Run Code Online (Sandbox Code Playgroud)