为什么 Array.Length 是 int,而不是 uint

发布于 2024-07-05 01:42:25 字数 204 浏览 15 评论 0原文

为什么 Array.Length 是 int,而不是 uint。 这让我很困扰(只是有点),因为长度值永远不可能是负数。

这也迫使我在我自己的类上使用 int 作为长度属性,因为当你 指定一个 int 值,这需要显式转换...

所以最终的问题是:无符号 int (uint) 有什么用处? 甚至微软似乎也不使用它们。

Why is Array.Length an int, and not a uint. This bothers me (just a bit) because a length value can never be negative.

This also forced me to use an int for a length-property on my own class, because when you
specify an int-value, this needs to be cast explicitly...

So the ultimate question is: is there any use for an unsigned int (uint)? Even Microsoft seems not to use them.

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

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

发布评论

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

评论(5

在巴黎塔顶看东京樱花 2024-07-12 01:42:25

我认为这也可能与简化较低级别的事情有关,因为如果 Array.Length 是无符号的,并且添加到负整数(二进制补码),那么 Array.Length 在某些时候当然会被添加到负数,可能会出现混乱的结果。

I think it also might have to do with simplifying things on a lower level, since Array.Length will of course be added to a negative number at some point, if Array.Length were unsigned, and added to a negative int (two's complement), there could be messy results.

家住魔仙堡 2024-07-12 01:42:25

似乎没有人回答“终极问题”。

我相信无符号整数的主要用途是提供与外部系统(P/Invoke 等)的更轻松的接口,并满足移植到 .NET 的各种语言的需求。

Looks like nobody provided answer to "the ultimate question".

I believe primary use of unsigned ints is to provide easier interfacing with external systems (P/Invoke and the like) and to cover needs of various languages being ported to .NET.

情深已缘浅 2024-07-12 01:42:25

通常,整数值是有符号的,除非您明确需要无符号值。 这只是它们的使用方式。 我可能不同意这个选择,但事实就是如此。

目前,由于当今典型的内存限制,如果您的数组或类似的数据结构需要 UInt32 长度,您应该考虑其他数据结构。

对于字节数组,Int32 将为您提供 2GB 的值

Typically, integer values are signed, unless you explicitly need an unsigned value. It's just the way they are used. I may not agree with that choice, but that's just the way it is.

For the time being, with todays typical memory constraints, if your array or similar data structure needs an UInt32 length, you should consider other data structures.

With an array of bytes, Int32 will give you 2GB of values

撩动你心 2024-07-12 01:42:25

Unsigned int 不符合 CLS,因此会将该属性的使用限制为那些实现 UInt 的语言。

参见这里:

框架1.1

.NET Framework 类库简介

Framework 2.0

.NET Framework 类库概述< /p>

Unsigned int isn't CLS compliant and would therefore restrict usage of the property to those languages that do implement a UInt.

See here:

Framework 1.1

Introduction to the .NET Framework Class Library

Framework 2.0

.NET Framework Class Library Overview

故人的歌 2024-07-12 01:42:25

原因有很多:

  • uint 不符合 CLS,因此使内置类型(数组)依赖于它会出现问题。
  • 最初设计的运行时禁止堆上的任何对象占用超过 2GB 的内存。 由于小于或等于此限制的最大大小的数组将是 new byte[int.MaxValue],因此人们对于能够生成正数但非法的数组长度感到困惑。
    • 请注意,此限制已在4.5 版本,但标准长度 int 仍然保留。
  • 从历史上看,C# 继承了 C 和 C++ 的大部分语法和约定。 在这些数组中只是简单的指针算术,因此负数组索引是可能的(尽管通常是非法和危险的)。 由于许多现有代码假设数组索引是有符号的,这将是一个因素
  • 在相关说明中,在 C/C++ 中对数组索引使用有符号整数意味着与这些语言和非托管函数的互操作需要在这些语言和非托管函数中使用整数无论如何,这可能会因不一致而造成混乱。
  • BinarySearch 实现(许多算法中非常有用的组件)依赖于能够使用 int 的负范围来指示未找到该值以及应插入此类值的位置来保持排序。
  • 当对数组进行操作时,您可能希望对现有索引采用负偏移量。 如果您使用的偏移量将使您超过使用单位的数组的开头,那么环绕行为将使您的索引可能合法(因为它是正数)。 使用 int 时,结果将是非法的(但安全,因为运行时会防止读取无效内存)

Many reasons:

  • uint is not CLS compliant, thus making a built in type (array) dependent on it would have been problematic
  • The runtime as originally designed prohibits any object on the heap occupying more than 2GB of memory. Since the maximum sized array that would less than or equal to this limit would be new byte[int.MaxValue] it would be puzzling to people to be able to generate positive but illegal array lengths.
  • Historically C# inherits much of its syntax and convention from C and C++. In those arrays are simply pointer arithmetic so negative array indexing was possible (though normally illegal and dangerous). Since much existing code assumes that the array index is signed this would have been a factor
  • On a related note the use of signed integers for array indexes in C/C++ means that interop with these languages and unmanaged functions would require the use of ints in those circumstances anyway, which may confuse due to the inconsistency.
  • The BinarySearch implementation (a very useful component of many algorithms) relies on being able to use the negative range of the int to indicate that the value was not found and the location at which such a value should be inserted to maintain sorting.
  • When operating on an array it is likely that you would want to take a negative offset of an existing index. If you used an offset which would take you past the start of the array using unit then the wrap around behaviour would make your index possibly legal (in that it is positive). With an int the result would be illegal (but safe since the runtime would guard against reading invalid memory)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文