c# - 为什么 decimal 不是原始类型?

标签 c# .net vb.net

为什么 decimal 不是原始类型?

Console.WriteLine(typeof(decimal).IsPrimitive);

输出false

它是一种基本类型,是语言规范的一部分,但不是原始类型。哪些基本类型在框架中表示 decimal?例如,int 有一个类型为 int 的字段 m_valuedouble 有一个类型为 double 的字段 m_valuedecimal 不是这种情况。它似乎由一堆 int 表示,但我不确定。

为什么它看起来像原始类型,behaves like a primitive type (在 couple of cases 中除外)但不是原始类型?

最佳答案

虽然不是直接的答案,IsPrimitive 的文档列出了基本类型是什么:

http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx

这里问了一个类似的问题:

http://bytes.com/topic/c-sharp/answers/233001-typeof-decimal-isprimitive-false-bug-feature

引用自 Jon Skeet 的回答:

The CLR doesn't need to have any intrinsic knowledge about the decimal type - it treats it just as another value type which happens to have overloaded operators. There are no IL instructions to operate directly on decimals, for instance.

对我来说,似乎 decimal 是一种必须存在于希望符合 CLS/CLI 的语言/运行时的类型(因此被称为“原始”,因为它是一个基础带有关键字支持的类型),但实际实现并不要求它是真正的“原始”(因为在 CLR 中并不认为它是原始数据类型)。

关于c# - 为什么 decimal 不是原始类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13471941/

相关文章:

c# - UWP 无法使用自定义 OSK 在 webview 的文本框中输入数据

c# - RestSharp 超时不起作用

sql-server - 如何从 Visual Basic.Net 调用表值函数以及如何存储结果

c# - 在给定的时间间隔内运行代码

mysql - SQL插入没有结果

c# - 当键不存在时,带有 When.NotExists 的 StringSetAsync 返回 false

c# - 调用 .NavigateToUrl(Uri uri) 时 BrowserWindow.Uri 属性未更新

c# - 结构中字符串的管理

.net - 取消数据绑定(bind)控件验证事件不会抑制更新数据源的尝试

c# - 构建错误: The type or namespace name 'TweetSharp' could not be found