.NET原语和类型层次结构,为什么要这样设计?
我想了解为什么.NET上有九种整数类型:Char
、Byte
、SByte
、Int16
、UInt16
、Int32
、UInt32
、Int64
和 UInt64
;加上其他数字类型:Single
、Double
、Decimal
;所有这些类型都没有任何关系。
当我第一次开始用 C# 编码时,我想“酷,有一个 uint
类型,当不允许负值时我将使用它”。然后我意识到没有API使用uint
而是使用int
,并且uint
不是从int
派生的,所以需要进行转换。
这些类型的实际应用是什么?为什么不使用 integer
和 positiveInteger
呢?这些是我能理解的类型。一个人的年龄(以岁为单位)是一个 positiveInteger
,并且由于 positiveInteger
是 integer
的子集,因此每当 integer
时都需要进行转换代码> 是预期的。
以下是 XPath 2.0 和 XQuery 1.0 中的类型层次结构图。如果您查看 xs:anyAtomicType
下,您可以看到数字层次结构 decimal
> > 整数
> 长
> int
> > 短
> 字节
。为什么.NET不是这样设计的?新框架“奥斯陆”会有什么不同吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是因为底层硬件破坏了该类层次结构。很多时候(也许令人惊讶)您关心
UInt32
是 4 字节大且无符号的,因此UInt32
不是 一种Int32
的类型,Int32
也不是Int64
的类型。您几乎总是关心
int
和float
之间的区别。从根本上说,继承和继承类层次结构与数学集合包含不同。
UInt32
可以保存的值是Int64
可以保存的值的严格子集这一事实并不意味着UInt32
是一种类型Int64
。不太明显的是,Int32
不是Int64
的类型 - 尽管它们之间没有概念上的差异,但它们的底层表示不同(4 字节与 8 字节)。小数
甚至更加不同。XPath 是不同的:所有数字类型的表示形式基本上是相同的 - ASCII 数字字符串。在那里,
short
和long
之间的区别是可能的范围之一,而不是表示形式 - “123”都是 a 的有效表示形式short
和具有相同值的long
的有效表示。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 aUInt32
is not a kind ofInt32
, nor is anInt32
a type ofInt64
.And you almost always care about the difference between an
int
and afloat
.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 anInt64
can hold does not mean that aUInt32
is a type ofInt64
. Less obviously, anInt32
is not a type ofInt64
- 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 along
is one of possible range rather than representation - "123" is both a valid representation of ashort
and a valid representation of along
with the same value.小数适用于需要精度(基本上是金钱)的计算。
请参阅此处:http://msdn.microsoft.com/ en-us/library/364x0z75(VS.80).aspx
Singles/Doubles 与小数不同,因为它们是近似值(基本上用于科学计算)。
这就是为什么他们没有关系。
至于字节和字符,它们完全不同:字节是 0-255,而字符是字符,因此可以存储 unicode 字符(它们的数量远远超过 255 个!)
Uint 和 int 则不然自动转换,因为它们可以存储彼此不可能存储的值(uint 的正范围是 int 的两倍)。
一旦你掌握了这一切的窍门,它实际上确实很有意义。
至于你的年龄,我只是使用 int ;)
Decimal is intended for calculations that need precision (basically, money).
See here: http://msdn.microsoft.com/en-us/library/364x0z75(VS.80).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 ;)