开发者

C# Numeric Data Type Naming

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


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.


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.


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:

  • Byte denotes 8 bits of data, says nothing about the format of the data.
  • Character denotes some data that stores a representation of a single text character.
  • "UINT8"/"INT8" denotes 8 bits of data, in signed or unsigned format, storing numeric integer values.


It really just comes down to being intuitive versus being consistent. It probably would have been cleaner if the .NET Framework used System.UInt8 and System.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 an int8 and a byte is called an unsigned int8, short is called int16, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜