.NET原语和类型层次结构,为什么要这样设计?

发布于 2024-08-09 20:21:28 字数 1070 浏览 5 评论 0原文

我想了解为什么.NET上有九种整数类型:CharByteSByteInt16UInt16Int32UInt32Int64UInt64;加上其他数字类型:SingleDoubleDecimal;所有这些类型都没有任何关系。

当我第一次开始用 C# 编码时,我想“酷,有一个 uint 类型,当不允许负值时我将使用它”。然后我意识到没有API使用uint而是使用int,并且uint不是从int派生的,所以需要进行转换。

这些类型的实际应用是什么?为什么不使用 integerpositiveInteger 呢?这些是我能理解的类型。一个人的年龄(以岁为单位)是一个 positiveInteger,并且由于 positiveIntegerinteger 的子集,因此每当 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?

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2024-08-16 20:21:28

我的猜测是因为底层硬件破坏了该类层次结构。很多时候(也许令人惊讶)您关心 UInt32 是 4 字节大且无符号的,因此 UInt32 不是 一种Int32 的类型,Int32 也不是 Int64 的类型。

您几乎总是关心 intfloat 之间的区别。

从根本上说,继承和继承类层次结构与数学集合包含不同。 UInt32 可以保存的值是 Int64 可以保存的值的严格子集这一事实并不意味着 UInt32 是一种类型Int64。不太明显的是,Int32 不是 Int64 的类型 - 尽管它们之间没有概念上的差异,但它们的底层表示不同(4 字节与 8 字节)。 小数甚至更加不同。

XPath 是不同的:所有数字类型的表示形式基本上是相同的 - ASCII 数字字符串。在那里,shortlong 之间的区别是可能的范围之一,而不是表示形式 - “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 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.

耳根太软 2024-08-16 20:21:28

小数适用于需要精度(基本上是金钱)的计算。
请参阅此处: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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文