作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要编写一个通用类,其中类型参数必须是实现 ICollection<T>
的东西. MyClass
内部我需要集合的项目类型(在标记为 ???
的代码片段中)。
class MyClass<TCollection> where TCollection : ICollection<???>
{
// ...
public void store(??? obj) { /* put obj into collection */ }
// ...
}
通常集合实际上是一本字典。有时,它会像列表一样简单。
我完全知道如何在 C++ 中执行此操作。我将如何做到这一点是 C#?
最佳答案
好吧,您通常会使用另一个类型参数来执行此操作:
class MyClass<TCollection, TElement> where TCollection : ICollection<TElement>
{
// ...
public void store(TElement obj) { }
// ...
}
在这种情况下你真的需要 TCollection
吗?你能用 TElement
凑合一下吗?
关于c# - C# : restricting the type parameter to be a collection 中的泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1642785/
我是一名优秀的程序员,十分优秀!