为什么 unsigned int 不符合 CLS?
为什么无符号整数不符合 CLS?
我开始认为类型规范只是为了性能而不是为了正确性。
Why are unsigned integers not CLS compliant?
I am starting to think the type specification is just for performance and not for correctness.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无符号整数不符合 CLS,因为它们在某些语言之间不可互操作。
Unsigned integers are not CLS compliant because they're not interoperable between certain languages.
无符号整数在现实生活中不会给你带来那么多好处,但是拥有超过一种类型的整数会给你带来痛苦,所以很多语言只有有符号整数。
符合 CLS 的目的是允许在多种语言中使用一个类...
请记住,没有人让您符合 CLS。
您仍然可以在内使用无符号整数 。方法,或作为私有方法的参数,因为它只是符合 CLS 限制的公共 API。
Unsigned int's don't gain you that much in real life, however having more than 1 type of int gives you pain, so a lot of languages only have singed ints.
CLS compliant is aimed at allowing a class to be made use of from lots of languages…
Remember that no one makes you be CLS compliant.
You can still use unsigned ints within a method, or as parms to a private method, as it is only the public API that CLS compliant restricts.
我怀疑,部分问题在于 C 中的无符号整数类型需要充当抽象代数环的成员而不是数字 [这意味着,例如,如果无符号 16 位整数变量等于 0 ,递减它需要来产生 65,535,如果它等于 65,535,则需要递增它来产生零。] 有时这种行为非常有用,但是数字类型表现出这种行为可能会违背了某些语言的精神。 我推测省略无符号类型的决定可能早于支持已检查和未检查数字上下文的决定。 就我个人而言,我希望无符号数和代数环有单独的整数类型; 将一元减运算符应用于无符号 32 位数字应产生 64 位有符号结果 [对除零以外的任何值取反将产生负数],但将一元减运算符应用于环类型应产生该环内的加法逆元。
无论如何,无符号整数不符合 CLS 的原因是 Microsoft 决定语言不必支持无符号整数才能被视为“CLS 兼容”。
Part of the issue, I suspect, revolves around the fact that unsigned integer types in C are required to behave as members of an abstract algebraic ring rather than as numbers [meaning, for example, that if an unsigned 16-bit integer variable equals zero, decrementing it is required to yield 65,535, and if it's equal to 65,535 then incrementing it is required to yield zero.] There are times when such behavior is extremely useful, but numeric types exhibit such behavior may have gone against the spirit of some languages. I would conjecture that the decision to omit unsigned types probably predates the decision to support both checked and unchecked numeric contexts. Personally, I wish there had been separate integer types for unsigned numbers and algebraic rings; applying a unary minus operator to unsigned 32-bit number should yield a 64-bit signed result [negating anything other than zero would yield a negative number] but applying a unary minus to a ring type should yield the additive inverse within that ring.
In any case, the reason unsigned integers are not CLS compliant is that Microsoft decided that languages didn't have to support unsigned integers in order to be considered "CLS compatible".
并非所有语言都有无符号整数的概念。 例如,VB 6 没有无符号整数的概念,我怀疑这促使 VB7/7.1 的设计者决定不实现(现在已在 VB8 中实现)。
去引用:
更新:几年前我确实想知道这个问题,虽然我不明白为什么 UInt 不能进行类型安全验证,但我猜 CLS 人员必须在某个地方设置一个截止点来确定最低基线是多少支持的值类型数量。 另外,当您考虑从长远来看,越来越多的语言被移植到 CLR 时,如果绝对没有概念,为什么还要强迫它们实现无符号整数以获得 CLS 合规性呢?
Not all languages have the concept of unsigned ints. For example VB 6 had no concept of unsigned ints which I suspect drove the decision of the designers of VB7/7.1 not to implement as well (it's implemented now in VB8).
To quote:
Update: I did wonder about this some years back, and whilst I can't see why a UInt wouldn't be type safety verifiable, I guess the CLS guys had to have a cut off point somewhere as to what would be the baseline minimum number of value types supported. Also when you think about the longer term where more and more languages are being ported to the CLR why force them to implement unsigned ints to gain CLS compliance if there is absolutely no concept, ever?