C# 为什么 Array.Length 是 int 而不是 uint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6301/
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:09  来源:igfitidea点击:

Why is Array.Length an int, and not an uint

提问by doekman

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

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

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

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

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

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

采纳答案by Kev

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

Unsigned int 不符合 CLS,因此会将属性的使用限制为那些确实实现了UInt.

See here:

看这里:

Framework 1.1

框架 1.1

Introduction to the .NET Framework Class Library

.NET Framework 类库简介

Framework 2.0

框架 2.0

.NET Framework Class Library Overview

.NET Framework 类库概述

回答by Lasse V. Karlsen

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.

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

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

使用字节数组,Int32 将为您提供 2GB 的值

回答by Kev

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.

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

回答by Constantin

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.

我相信 unsigned int 的主要用途是提供更容易与外部系统(P/Invoke 等)的接口,并满足移植到 .NET 的各种语言的需求。

回答by ShuggyCoUk

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