为什么 .NET Framework 不使用无符号数据类型?

发布于 2024-09-27 03:59:12 字数 364 浏览 0 评论 0原文

可能的重复:
为什么 Array.Length 是 int,而不是 uint

.NET Framework 不使用无符号数据类型背后是否有原因?

我不应该在我的代码中采用它们吗?例如,List<> 的 Count 属性是一个整数。计数不能为负数,那么为什么不应该将其定义为 uint 呢?即使我知道计数不能为负数,我也应该只使用 int 吗?

Possible Duplicate:
Why is Array.Length an int, and not an uint

Is there is a reason behind it .NET Framework not using unsigned data types?

Shouldn't I be adopting them in my code, but for example, the Count property of a List<> is an int. You can't have a negative count, so why shouldn't it be defined as a uint? Should I use only int's even though I know the count can not be negative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三生殊途 2024-10-04 03:59:12

无符号数字类型不CLS 兼容,因此不应将它们用于< em>任何 API - 尤其是.NET 框架。

基本上,符合 CLS 的代码仅使用所有 .NET 语言中可用的类型。某些语言(如 VB.NET)不支持无符号数字类型。

Unsigned numeric types are not CLS compliant so they should not be used for any API - especially the .NET framework.

Basically, CLS compliant code only utilizes types that are available in all .NET languages. Some languages (like VB.NET) does not support unsigned numeric types.

偷得浮生 2024-10-04 03:59:12

最基本的答案是.NET框架设计者选择将有符号整数作为BCL(基类库)和CLS(公共语言语法)规范的一部分,而没有选择将无符号整数作为其中的一部分。

对于这个决定背后的原因,最终您必须询问微软。我想埃里克·利珀特(Eric Lippert)可以在这里插话并提供更彻底的解释。

归结为以下事实:

  • 您不能(或至少不能)在 intuint 之间进行隐式转换,这意味着必须显式进行转换使用强制转换语法
  • 如果不会增加代码的可读性,最好避免不必要的强制转换语法
  • int 的上限对于我们在代码中处理的大多数数字来说已经足够了

将它们放在一起意味着: int 类型在大多数情况下都能满足目的。

The most basic answer is that the .NET framework designers chose to have signed integers part of the BCL (base class library) and CLS (common language syntax) specification, whereas they did not chose to make unsigned integers part of it.

For the reasoning behind that decision, ultimately you'd have to ask Microsoft. I would imagine that Eric Lippert could chime in here with a more thorough explanation.

It comes down to the following facts:

  • You can't (or at least don't) have an implicit conversion between int and uint, meaning that conversions must be made explicitly using cast syntax
  • It's best to avoid unnecessary casting syntax if it isn't adding to the readability of the code
  • The upper bounds of int is sufficient for most numbers that we deal with in code

Putting those together means that the int type serves the purpose in most cases.

佼人 2024-10-04 03:59:12

无符号数不符合 CLS,因为有些语言不支持这些类型。我说的是更正,但VB.NET不支持无符号类型。这意味着,如果将公共成员声明为无符号类型,则无法在 VB.NET 或任何其他不支持无符号类型的 .NET 语言中使用您的程序集。

Unsigned numbers are not CLS compliant because there are languages that do not support those types. I speak under correction, but VB.NET does not support unsigned types. This means that if you declare an public member as an unsigned type, then your assembly cannot be used from VB.NET or any other .NET language that does not support unsigned types.

暖伴 2024-10-04 03:59:12

一些支持 .NET 的语言确实允许无符号整数。例如,在 C# 中,您可以使用 uint。

但是,由于它们不符合 CLS,如果您将它们暴露在程序集之外,那么您可能会发现使用不同语言的其他开发人员在使用您的类时会遇到问题。

总而言之...请随意使用它们,但使用 private 或 inside 关键字将它们压缩在您的类中。

Some .NET aware languages do allow unsigned ints. E.g. in C# you can use uint.

However, as they not CLS compliant if you expose them outside of your assembly then you might find that other developers using a different language will have problems using your classes.

So in summary... Feel free to use them, but keep them zipped up inside your classes using either the private or internal keywords.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文