有没有限制我的泛型方法来数值类型约束? [英] Is there a constraint that restricts my generic method to numeric types?
问题描述
谁能告诉我,如果有泛型的方式来限制一个泛型类型参数 T
仅
-
的Int16
-
的Int32
-
的Int64
-
UINT16
-
UInt32的
-
UINT64
我知道了其中,
关键字,但无法找到一个接口的仅这些类型,
是这样的:
静态布尔IntegerFunction< T>(T值),其中T:INumeric
Hejlsberg为描述了为何不实施该功能接受记者采访时和Bruce Eckel的。
我必须承认,虽然,我不知道他是怎么想他提出的解决方法会奏效。他的建议是,以算术运算推迟到一些其他的泛型类(阅读我们的采访!)。怎样帮助呢?恕我直言,没有太大的。
Can anyone tell me if there is a way with generics to limit a generic type argument T
to only:
Int16
Int32
Int64
UInt16
UInt32
UInt64
I'm aware of the where
keyword, but can't find an interface for only these types,
Something like:
static bool IntegerFunction<T>(T value) where T : INumeric
Hejlsberg has described the reasons for not implementing the feature in an interview with Bruce Eckel.
I have to admit, though, that I don't know how he thinks his proposed workaround will work. His proposal is to defer arithmetic operations to some other generic class (read the interview!). How does this help? IMHO, not much.
这篇关于有没有限制我的泛型方法来数值类型约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!