简体   繁体   English

将类型化的参数定义为定义运算符重载的参数

[英]Defining Typed Parameters as the Ones that Define Operator Overloading

Is there anyway to define type parameter T as the ones that define operator overloading? 无论如何,是否将类型参数T定义为定义运算符重载的参数? Take for example, I need to create a generic Add function with the parameter T . 例如,我需要使用参数T创建一个通用的Add函数。 Obviously, the T must defined the + operator, ie, 显然,T必须定义+运算符,即

 public static T operator +(T c1,T c2)
 {
// plus operation
 }

Is there anyway to constraint T so that it is restricted to the types that overload the operators? 无论如何,约束T会被限制为使运算符超载的类型吗?

No. 没有。

Keeping it short as this question has been duplicated many times. 简短起见,因为这个问题已经重复了很多次。

eg here 例如在这里

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

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