publicclassMyGenericClass<T> whereT : SomeType
{
// Class implementation
}
类型约束:您可以对类型参数应用各种约束,例如: 类约束(where T : class):确保类型参数必须是引用类型(类或接口)。 结构约束(where T : struct):确保类型参数必须是值类型(结构)。 构造函数约束(where T : new()):确保类型参数必须具有无参数构造函数。 接口约束(where T : IMyInterface):确保类型参数必须实现特定接口。 基类约束(where T : MyBaseClass):确保类型参数必须继承自特定的基类。