views:

189

answers:

4

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?

+8  A: 

No.

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

Joey
thanks for your quick answer!
google
A: 

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.

Tor Haugen
With all due respect, it isn't "silly" - it is a perfectly valid question, especially if you come from a java background where the difference is boxed vs unboxed - i.e. a big difference. It **happens** that in C# they are the same.
Marc Gravell
+1  A: 

This question should explain things to you

String vs string in C#

astander
The OP is referring to the c# decimal alias vs the System.Decimal type
Richard Szalay
Richard: Yes, and the situation is the same for `decimal` or for `long` or `int`, etc.
Joey
And the answer to that question states this.
astander
+3  A: 

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.

Guffa