本文介绍了有没有限制我的泛型方法来数值类型约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我,如果有泛型的方式来限制一个泛型类型参数 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.

这篇关于有没有限制我的泛型方法来数值类型约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:49