限制类,允许在C#加法/减法运算(+/-) [英] Constrain type to allow addition/subtraction operations (+/-) in C#

查看:240
本文介绍了限制类,允许在C#加法/减法运算(+/-)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能的。

public interface Foo<TBar>
  where TBar : (can use the '+' and '-' operators)



谢谢

Thanks.

推荐答案

您可以创建一个类型的重载这两个运营商然后你的约束泛型类型吧。但是,您不能限制你的泛型参数要求对任何类型的任意一个特设的基础上重载这样的运营商。

You can create a type Foo that overloads those two operators and then constrain your generic type to it. You cannot however constrain your generic parameter to require that any arbitrary type overloads such operators on an ad-hoc basis.

这篇关于限制类,允许在C#加法/减法运算(+/-)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆