我有一个看起来像这样的通用方法:
public int GetCount<T>(T collection) where T: ICollection
{
return collection.Count;
}
现在我希望能够调用这个方法,其中集合参数可以是 aList<T>
或 a HashSet<T>
。当前代码不满足这一点,因为我要传递的参数不继承ICollection
接口。现在有什么方法可以通过简单的约束来实现吗?
我有一个看起来像这样的通用方法:
public int GetCount<T>(T collection) where T: ICollection
{
return collection.Count;
}
现在我希望能够调用这个方法,其中集合参数可以是 aList<T>
或 a HashSet<T>
。当前代码不满足这一点,因为我要传递的参数不继承ICollection
接口。现在有什么方法可以通过简单的约束来实现吗?