tags:

views:

240

answers:

2

I would like to understand why on .NET there are nine integer types: Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, and UInt64; plus other numeric types: Single, Double, Decimal; and all these types have no relation at all.

When I first started coding in C# I thought "cool, there's a uint type, I'm going to use that when negative values are not allowed". Then I realized no API used uint but int instead, and that uint is not derived from int, so a conversion was needed.

What are the real world application of these types? Why not have, instead, integer and positiveInteger ? These are types I can understand. A person's age in years is a positiveInteger, and since positiveInteger is a subset of integer there's so need for conversion whenever integer is expected.

The following is a diagram of the type hierarchy in XPath 2.0 and XQuery 1.0. If you look under xs:anyAtomicType you can see the numeric hierarchy decimal > integer > long > int > short > byte. Why wasn't .NET designed like this? Will the new framework "Oslo" be any different?

alt text

+3  A: 

Decimal is intended for calculations that need precision (basically, money). See here: http://msdn.microsoft.com/en-us/library/364x0z75%28VS.80%29.aspx

Singles/Doubles are different to decimals, because they're intended to be an approximation (basically, for scientific calculations).

That's why they're not related.

As for bytes and chars, they're totally different: a byte is 0-255, whereas a char is a character, and can hence store unicode characters (there are a lot more than 255 of them!)

Uints and ints don't convert automatically, because they can each store values that are impossible for the other (uints have twice the positive range of ints).

Once you get the hang of it all, it actually does make a lot of sense.

As for your ages thing, i'd simply use an int ;)

Chris
I understand why these numeric types are not related, my question is, why was .NET designed like this? I'm starting to think it's for compatibility with RDBMS. Do you use uint in your apps?
Max Toro
Never had the need to use uint personally. And if you're ever wondering 'why was feature X in .net designed like this?' the usual answer is 'because its like that in Java' ;)
Chris
One reason that int is often used over uint is that uint is not compliant with the Common Language Specification.
Porges
Decimal does NOT have "unlimited accuracy and length". Decimal arithmetic is approximate, not exact. The difference between decimal and double is that they have different values that can be represented without error, different precisions, and different ranges.
Eric Lippert
I'll be jiggered - you're right! I must have been getting it mixed up with ruby's big numbers: http://msdn.microsoft.com/en-us/library/364x0z75(VS.80).aspx
Chris
+2  A: 

My guess would be because the underlying hardware breaks that class hierarchy. There are (perhaps surprisingly) many times when you care that a UInt32 is a 4 bytes big and unsigned, so a UInt32 is not a kind of Int32, nor is an Int32 a type of Int64.

And you almost always care about the difference between an int and a float.

Fundamentally, inheritance & the class hierarchy are not the same as mathematical set inclusion. The fact that the values a UInt32 can hold are a strict subset of the values an Int64 can hold does not mean that a UInt32 is a type of Int64. Less obviously, an Int32 is not a type of Int64 - even though there's no conceptual difference between them, their underlying representations are different (4 bytes versus 8 bytes). Decimals are even more different.

XPath is different: the representations for all the numeric types are fundamentally the same - a string of ASCII digits. There, the difference between a short and a long is one of possible range rather than representation - "123" is both a valid representation of a short and a valid representation of a long with the same value.

RAOF
can you elaborate on 'underlying hardware breaks that class hierarchy'?
Max Toro
Conceptually, the hierarchy in your diagram is correct - a byte is a subset of short is a subset of int, etc. This works for XPath, since it's dealing with XML, where the value 123 could be represented by the byte "123", the short "123", or the long "123" - the representations are exactly the same. Here, the type is a constraint on the possible values, but doesn't change the representation.For C#, the value 123 can be represented in memory as the byte, 0111 1011, the short 0000 0000 0111 1011, the int... These representations are a different size, and the hardware has to care about that.
RAOF
I see. Then, why do we need different representations? is it to make programs more efficient?
Max Toro
RAOF
Thanks for your help. I wish I could program on a higher level, I thought C# was high enough. I'm happy to know now that the "M" language does have a numeric type hierarchy: Number > Decimal > Integer > Unsigned. http://msdn.microsoft.com/en-us/library/dd285273.aspx
Max Toro