在 MSDN 上 - C# 编程指南 Constraints on Type Parameters ,它说:
where T : interface_name
The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic.
有人可以解释一下,拥有通用接口(interface)意味着什么吗?并解释它如何成为约束以及它提供了什么?
非常感谢简单
示例和简单
解释。
非常感谢:)
最佳答案
您可以使用通用接口(interface)作为约束。例如:
class MyClass<T> where T:IEnumerable<string>
您甚至可以将您定义的类型的通用参数替换为您的约束:
class MyClass<T> where T:IEnumerable<T>
关于C# 泛型 : What is generic constraining interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6298582/