简体   繁体   English

比较T与Int64或Double

[英]Compare T to Int64 or Double

I have the a method with the following signature; 我有一个带有以下签名的方法;

private static void CheckValue<T>(ref Double result, Int64 value, String condition, T checkValue) where T : class{};

The checkValue can be a Double or an Int64. checkValue可以是Double或Int64。 However it gives the following error; 但是,它产生以下错误;

Operator '>=' cannot be applied to operatands of type 'long' and 'T'

Because I want a generic function and do not want to define two functions (One with the Int64 checkValue and one with the Double checkValue signature), I came up with T. 因为我想要一个泛型函数,并且不想定义两个函数(一个带有Int64 checkValue,另一个带有Double checkValue签名),所以我想到了T。

I used the where T : class, this way it limits T to classes and can use operators. 我使用where T:class,这样将T限制为类并可以使用运算符。 (Found this on the following topic): How to solve Operator '!=' cannot be applied to operands of type 'T' and 'T' (在以下主题上找到此内容): 如何求解运算符'!='不能应用于类型'T'和'T'的操作数

I can imagine that this works if I can restrain T to only Int64 or Double. 我可以想象如果我可以将T限制为仅Int64或Double,则此方法有效。 However if I change the following in the signature: 但是,如果我在签名中更改以下内容:

where T : class, Int64, Double

It gives the same error. 它给出了相同的错误。

@Edit - 06-08-2015 11:16 My bad, If I run the prior it gives an error about the Int64 and Double in the where clause. @Edit-06-08-2015 11:16我不好,如果我运行先前的命令,它将在where子句中给出有关Int64和Double的错误。 (It is not a valid constraint) (这不是有效的约束)

The checkValue can be a Double or an Int64 checkValue可以是Double或Int64

No it can't. 不,它不能。 You've got where T : class as a constraint to insist that T is a reference type, so T can be neither of those things. 您已经有了where T : class作为约束来坚持认为T是引用类型,因此T既不是这些东西。 You in fact what to insist upon the opposite: 您实际上要坚持相反的做法:

private static void CheckValue<T>(ref Double result, Int64 value, String condition, T checkValue) where T : struct
{
}

Operator '>=' cannot be applied to operatands of type 'long' and 'T' 运算符'> ='不能应用于类型'long'和'T'的运算符

Well it can't. 好吧,那不可能。 You need to have a specific type which >= can be used with. 您需要具有可以与>=一起使用的特定类型。

You can though depend upon the fact that both long and double implement IComparable . 但是,您可以依赖longdouble实现IComparable的事实。 I've no idea what you actually want to do in your implementation, but the following works: 我不知道您在实现中实际要做什么,但是可以进行以下工作:

private static void CheckValue<T>(ref Double result, Int64 value, String condition, T checkValue) where T : struct, IComparable
{
  if(checkValue is long)
    result = checkValue.CompareTo(value) >= 0 ? 1.0 : -1.0;
  else if(checkValue is double)
    result = checkValue.CompareTo((double)value) >= 0 ? 1.0 : -1.0;
  else
    throw new Invalid‎OperationException();
}

Because I want a generic function and do not want to define two functions (One with the Int64 checkValue and one with the Double checkValue signature), I came up with T. 因为我想要一个泛型函数,并且不想定义两个函数(一个带有Int64 checkValue,另一个带有Double checkValue签名),所以我想到了T。

While the above works, I'd recommend you do have separate methods here. 尽管上述方法可行,但我建议您在这里使用单独的方法。 The result will be faster (no runtime check on type, and faster comparison call) and more type-safe (no chance of the runtime check on type failing). 结果将更快(没有类型的运行时检查,并且更快的比较调用)和更多类型安全(没有类型的运行时检查失败的机会)。 It's worth noting for comparison that many framework methods have overloads on different primitive types, even in methods that were first introduced to the framework after generics where added to .NET, for these reasons. 值得一提的是,由于这些原因,许多框架方法在不同的原始类型上都有重载,即使在将泛型添加到.NET之后首次引入框架的方法中,也是如此。

(I'd also note that using ref is often best avoided, especially since you aren't returning any other value forcing you into using ref . Could you instead accept Double result and return a double , with the replacement of the value happening where the call is made rather than inside this method?) (我还要注意,通常最好避免使用ref ,特别是因为您不会返回任何其他值来强迫您使用ref 。您是否可以接受Double result并返回double ,而替换值发生在而不是在此方法内进行调用?)

There is no need to implement it using generics as you are comparing a double or an int with an Integer value. 在将double或int与Integer值进行比较时,无需使用泛型来实现它。 But if you some implementation with generics , then Just have a signature like this. 但是,如果您使用泛型实现某些实现,则只需具有这样的签名即可。

 private static void CheckValue<T>(ref Double result, int value, String condition, T checkValue) where T : struct
        {
            if (value.Equals(checkValue))
                result=1; // do anything you want here
            else
                result=0;
        }

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

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