system.Decimal 是否比“decimal”使用更多内存?

发布于 2024-08-12 20:17:33 字数 77 浏览 4 评论 0原文

我听到有人说在 C# 中,大写十进制比小写十进制使用更多的内存,因为小写十进制被解析为小写十进制,这需要内存。

这是真的吗?

I heard someone say that in C#, capital Decimal is using more memory than lower case decimal, because Decimal is resolved to the lowercase decimal and that requires memory.

Is that true?

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

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

发布评论

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

评论(3

廻憶裏菂餘溫 2024-08-19 20:17:33

不。

decimal 只是 System.Decimal 的别名。它们完全相同,并且别名在编译时解析。

No.

decimal is simply an alias for System.Decimal. They're exactly the same and the alias is resolved at compile-time.

半透明的墙 2024-08-19 20:17:33

不,那不是真的。

decimal 关键字是 System.Decimal 类型的别名。它们是完全相同的类型,因此没有内存差异,也没有性能差异。如果使用反射来查看编译后的代码,甚至无法判断源代码中是否使用了别名或系统类型。

不过,可以使用别名和系统类型的地方有两个区别:

  • decimal 别名始终是系统类型,不能以任何方式更改。 Decimal 标识符的使用依赖于导入 System 命名空间。系统类型的明确名称是 global::System.Decimal

  • 某些语言结构只接受别名,而不接受类型。我想不出 decimal 的示例,但是在指定枚举的基础类型时,您只能使用像 int 这样的语言别名,而不是像 int 这样的相应系统类型code>System.Int32.

No, that is not true.

The decimal keyword is an alias for the type System.Decimal. They are the exact same type, so there is no memory difference and no performance difference. If you use reflection to look at the compiled code, it's not even possible to tell if the alias or the system type was used in the source code.

There is two differences in where you can use the alias and the system type, though:

  • The decimal alias is always the system type and can not be changed in any way. The use of the Decimal identifier relies on importing the System namespace. The unambiguous name for the system type is global::System.Decimal.

  • Some language constructs only accept the alias, not the type. I can't think of an example for decimal, but when specifying the underlying type for an enum you can only use language aliases like int, not the corresponing system type like System.Int32.

怎会甘心 2024-08-19 20:17:33

不,那太愚蠢了。

在 C# 中,decimal 只是 Decimal 的同义词。编译器会将十进制声明视为 Decimal,编译后的代码将如同使用 Decimal 一样。

No. That's just silly.

In C#, decimal is just a synonym for Decimal. The compiler will treat decimal declarations as Decimal, and the compiled code will be as if Decimal was used.

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