【问题标题】:Generics with operators C# [duplicate]带有运算符 C# 的泛型 [重复]
【发布时间】:2014-01-01 13:47:05
【问题描述】:

我需要使用一个通用方法来减去两个操作数,就像这样,带有一些伪编码。

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?

【问题讨论】:

  • 不,不可能在 C# 中编写这样的约束。

标签: c# generics operators


【解决方案1】:

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

猜你喜欢
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
相关资源
最近更新 更多