是否有约束将我的通用方法限制为数字类型? [英] Is there a constraint that restricts my generic method to numeric types?

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

问题描述

任何人都可以告诉我是否有一种方法可以将泛型类型参数 T 限制为:




  • Int16

  • Int32 li>
  • Int64

  • UInt16 li>
  • UInt32

  • li>


我知道关键字,但是找不到接口

$ <$ p <$ p <$ p <$ p <$ p <$ p

c $ c> static bool IntegerFunction< T>(T value)其中T:INumeric


解决方案

Hejlsberg描述了未实施此功能的原因在采访中< a> with Bruce Eckel。



我不得不承认,我不知道他怎么认为他的建议的解决方法会奏效。他的建议是将算术运算推迟到其他通用类(阅读面试!)。这有什么帮助? IMHO,不多。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆