C# 为什么 unsigned int 不符合 CLS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 16:26:22  来源:igfitidea点击:

Why are unsigned int's not CLS compliant?

提问by doekman

Why are unsigned integers not CLS compliant?

为什么无符号整数不符合 CLS?

I am starting to think the type specification is just for performance and not for correctness.

我开始认为类型规范只是为了性能而不是为了正确性。

采纳答案by Kev

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).

并非所有语言都有无符号整数的概念。例如,VB 6 没有 unsigned int 的概念,我怀疑这促使 VB7/7.1 的设计者决定不再实现(它现在在 VB8 中实现)。

To quote:

报价:

http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx

The CLS was designed to be large enough to include the language constructs that are commonly needed by developers, yet small enough that most languages are able to support it. In addition, any language construct that makes it impossible to rapidly verify the type safety of code was excluded from the CLS so that all CLS-compliant languages can produce verifiable code if they choose to do so.

http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx

CLS 设计得足够大,以包含开发人员通常需要的语言结构,但又足够小,以至于大多数语言都能够支持它。此外,任何无法快速验证代码类型安全的语言结构都被排除在 CLS 之外,以便所有符合 CLS 的语言都可以生成可验证的代码,如果他们选择这样做的话。

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?

更新:几年前我确实想知道这个问题,虽然我不明白为什么 UInt 不能进行类型安全验证,但我猜 CLS 的人必须在某处有一个截止点来确定基线最小值是多少支持的值类型数。此外,当您考虑将越来越多的语言移植到 CLR 的长期来看时,如果绝对没有概念,为什么要强迫它们实现无符号整数以获得 CLS 合规性呢?

回答by Bryan Roth

Unsigned integers are not CLS compliant because they're not interoperable between certain languages.

无符号整数不符合 CLS,因为它们不能在某些语言之间互操作。

回答by Ian Ringrose

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.

无符号整数在现实生活中并没有给你带来太多好处,但是拥有超过 1 种类型的整数会给你带来痛苦,所以很多语言都只有签名整数。

CLS compliant is aimed at allowing a class to be made use of from lots of languages…

符合 CLS 的目的是允许使用多种语言的类……

Remember that no one makes you be CLS compliant.

请记住,没有人让您符合 CLS。

You can still use unsigned ints withina method, or as parms to a privatemethod, as it is only the public API that CLS compliant restricts.

您仍然可以方法中使用无符号整数,或作为私有方法的参数,因为它只是 CLS 兼容限制的公共 API。

回答by supercat

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 requiredto 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.

我怀疑,部分问题在于 C 中的无符号整数类型需要作为抽象代数环的成员而不是数字[意思是,例如,如果一个无符号的 16 位整数变量等于零, 递减它是必需的产生 65,535,如果它等于 65,535,则需要递增以产生零。] 有时这种行为非常有用,但数字类型表现出这种行为可能与某些语言的精神背道而驰。我猜想省略无符号类型的决定可能早于支持检查和未检查数字上下文的决定。就个人而言,我希望无符号数和代数环有单独的整数类型;将一元减号运算符应用于无符号 32 位数字应产生 64 位有符号结果 [否定除零以外的任何内容将产生负数] 但将一元减号应用于环类型应产生该环内的加法逆。

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".

在任何情况下,无符号整数不符合 CLS 的原因是 Microsoft 决定语言不必支持无符号整数才能被视为“兼容 CLS”。