C# 数字数据类型命名
查看 C# 数字数据类型,我注意到大多数类型都有有符号和无符号版本。我注意到,虽然“默认”整数、short 和 long 是有符号的,并且它们的无符号对应项是 uint、ushort 和 ulong; “默认”字节是无符号的,并且在 sbyte 中有一个有符号的对应字节。
只是出于好奇,为什么字节与其他的如此不同?这背后是否有特定的原因,或者“事情就是这样”?
希望这个问题不会因为我的措辞和过度使用引号而变得太混乱。呵呵..
Looking at the C# numeric data types, I noticed that most of the types have a signed and unsigned version. I noticed that whereas the "default" integer, short and long are signed, and have their unsigned counterpart as uint, ushort and ulong; the "default" byte is instead unsigned - and have a signed counterpart in sbyte.
Just out of curiosity, why is byte so different from the rest? Was there a specific reason behind this or it is "just the way things are"?
Hope the question isn't too confusing due to my phrasing and excessive use of quotes. Heh..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想说字节不被视为数字类型,而是定义了 8 位大小的结构。除了没有符号字节概念外,它还是无符号的。另一方面,数字首先被认为是有符号的,因此声明它们是无符号的,这是不常见的,需要前缀
[编辑]
忘记了有一个有符号字节(sbyte)。我认为这是相当历史和实际应用。 Int 比 UInt 更常见,而 byte 比 sbyte 更常见。
I would say a byte is not considered a numeric type but defines a structure with 8 bits in size. Besides there is no signed byte notion, it is unsigned. Numbers on the otherhand are firstly considered to be signed, so stating they are unsigned which is less common warrants the prefix
[EDIT]
Forgot there is a signed byte (sbyte). I suppose it is rather historical and practical application. Ints are more common than UInts and byte is more common than sbyte.
从历史上看,术语“字节”、“半字节”和“位”表示存储单元、助记符或代码……而不是数值。拥有负兆字节的内存或添加 ASCII 代码 1 和 2 并期望代码 3 有点愚蠢。在许多方面,不存在有符号的“字节”这样的东西。有时“事物”和“值”之间的界限非常模糊......就像大多数将字节视为事物和值的语言一样。
Historically the terms byte, nibble and bit indicate a unit of storage, a mnemonic or code...not a numeric value. Having negative mega-bytes of memory or adding ASCII codes 1 and 2 expecting code 3 is kinda silly. In many ways there is no such thing as a signed "byte". Sometimes the line between "thing" and "value" is very blurry....as with most languages that treat byte as a thing and a value.
更重要的是条款的某种程度的腐败。字节本质上并不是任何形式的数字,它只是一个存储单元。
然而,字节、字符和 8 位有符号/无符号整数的名称在它们可能不应该互换的地方可以互换使用:
与数据格式无关。
存储单个的表示
文本字符。
数据,有符号或无符号格式,
存储整数值。
It's more so a degree of corruption of the terms. A byte is not inherently numeric in any form, it's simply a unit of storage.
However, bytes, characters, and 8-bit signed/unsigned integers have had their names used interchangeably where they probably should not have:
nothing about the format of the data.
stores a representation of a single
text character.
data, in signed or unsigned format,
storing numeric integer values.
这实际上只是取决于直觉与一致性。如果 .NET Framework 使用 System.UInt8 和 System.Int8 来与其他整数类型保持一致,它可能会更清晰。但是,是的,这看起来确实有点武断。
就其价值而言,MSIL(所有 .NET 语言都编译为无论如何)更加一致,因为
sbyte
称为int8
和byte
称为unsigned int8
,short
称为int16
等。但是术语“字节”通常不用于描述数字类型,而是用于描述数字类型。 8 位的集合,例如在处理文件、序列化、套接字等时。例如,如果 Stream.Read 使用 System.Int8[] 数组,那么这将是一个看起来非常不寻常的 API。
It really just comes down to being intuitive versus being consistent. It probably would have been cleaner if the .NET Framework used
System.UInt8
andSystem.Int8
for consistency with the other integer types. But yeah it does seem a bit arbitrary.For what it's worth MSIL (which all .NET languages compile to anyhow) is more consistent in that a
sbyte
is called anint8
and abyte
is called anunsigned int8
,short
is calledint16
, etc.But the term byte is typically not used to describe a numeric type but rather a set of 8 bits such as when dealing with files, serialization, sockets, etc. For example if Stream.Read worked with a System.Int8[] array, that would be a very unusual looking API.