This question already has answers here:
Arithmetic operator overloading for a generic class in C#

(12 个回答)



How can I subtract two generic objects (T - T) in C# (Example: DateTime - DateTime)?

(6 个回答)


7年前关闭。




我需要使用一种通用方法来减去两个操作数,就像这样,带有一些伪编码。
public double GetResult<T>(T Arg1, T Arg2) : where T contains "-" operator
{
     return Arg1 - Arg2;
     //No, they are not value types, they are classes implementing the "-" operator
     //And eventually, they can be value types
}

那可能吗?就像有创建者类型的 where T = new() 一样,是否有 where T contains "-" operator

最佳答案

http://www.yoda.arachsys.com/csharp/miscutil/ 中的库包含用于对通用参数执行数学运算的类。

关于c# - 带有运算符的泛型 C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20572474/

10-13 08:54