简体   繁体   English

system.Decimal是否比'decimal'使用更多的内存?

[英]Does system.Decimal use more memory than 'decimal'?

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. 我听说有人说在C#中,大写的Decimal比小写的十进制使用的内存更多,因为Decimal被解析为小写的十进制,并且需要内存。

Is that true? 真的吗?

No. 没有。

decimal is simply an alias for System.Decimal . decimal只是System.Decimal的别名。 They're exactly the same and the alias is resolved at compile-time. 它们完全相同,并且别名在编译时解析。

No, that is not true. 不,那不是真的。

The decimal keyword is an alias for the type System.Decimal . decimal关键字是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. decimal别名始终是系统类型,不能以任何方式更改。 The use of the Decimal identifier relies on importing the System namespace. 使用Decimal标识符依赖于导入System名称空间。 The unambiguous name for the system type is global::System.Decimal . 系统类型的明确名称是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 . 我想不出decimal的示例,但是在为枚举指定基础类型时,只能使用int类的语言别名,而不能使用System.Int32类的对应系统类型。

No. That's just silly. 不,那太傻了。

In C#, decimal is just a synonym for Decimal. 在C#中,十进制只是Decimal的同义词。 The compiler will treat decimal declarations as Decimal, and the compiled code will be as if Decimal was used. 编译器会将十进制声明视为十进制,而编译后的代码将如同使用了十进制一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM