Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# integral types inconsistency

Tags:

c#

integral

I just started reading up on the C# language, and one of the first sections in my reading material is, naturally, variables and types.

In short order I came across the integral types table, which listed sbyte, byte, short, ushort, int, uint, long, ulong and char. The exception to the pairing here is char, so let's disregard it and have a look at the others.

The first pair, byte, prefixes the signed version with an "s", and leaves the unsigned version without a prefix. But for all the other pairs, the relation is reversed... the signed version is without prefix and the unsigned has a "u" prefix.

Is there a reason for this that I should be aware of, or is this just a design quirk?

Link for reference: http://msdn.microsoft.com/en-us/library/exx3b86w.aspx

like image 631
grimman Avatar asked Dec 03 '22 14:12

grimman


1 Answers

Bytes are conventionally treated as a pattern of bits rather than a normal number, and the "standard" values for a byte are 0 to 255. The signed version of byte is the abnormal case, with values treated as -128 to 127.

For all other numeric types the "standard" values are signed, as this tallies with our day-to-day intuitions about numbers (ie, that they can be negative as well as positive). For numeric types, the unsigned versions are the abnormal case.

(I don't think that I've ever used sbyte in any real-world code, and I use int, long etc much more often than I use their unsigned counterparts.)

like image 134
LukeH Avatar answered Dec 20 '22 05:12

LukeH