繁体   English   中英

如何定义具有两个通用类型和两个类型约束的方法?

[英]How can I define a method with two Generic types and two type constraints?

我的方法定义是

public ActionResult _Create3<T>() where T:IMyClass, new()

但我想定义两种通用类型

public ActionResult _Create3<T, G>(G content) where T:IMyClass, new()

G型也必须使用ImyClass接口,但我不知道在哪里定义两种类型!

例如,如果可能写:

public ActionResult _Create3<T, G>(G content) where {T:IMyClass, G:IMyClass}, new()

但得到错误。

谢谢你的回答

为该泛型类型添加另一个where 约束

public ActionResult _Create3<T, G>(G content) 
  where T : IMyClass, new()
  where G : IMyClass, new()

您可以定义多个where的多个泛型类型类似的约束:

public ActionResult _Create3<T, G>(G content) where T:IMyClass, new()
                                              where G:IMyClass, new()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM