[英]How to declare a method that returns a generic collection of “anything” (C#)
[英]How to declare a generic method with a generic collection?
例如
void f<C, T>(C<T> collection, T element) : where C has member method BindarySearch
我有两个问题:
您可以使用接口来强制所需的成员:
interface IHasBinarySearch<T> : ICollection<T> {
int BinarySearch(T item);
}
然后可以定义一个通用方法,如下所示:
void f<C, T>(C collection, T element) where C : IHasBinarySearch<T>
该接口已经是通用集合(通过扩展ICollection<T>
),因此无需再次在方法上指定约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.