tags:

views:

696

answers:

3

If someone could explain to me the difference between Decimal and decimal in C# that would be great.

In a more general fashion, what is the difference between the lower-case structs like decimal, int, string and the upper case classes Decimal, Int32, String.

Is the only difference that the upper case classes also wrap functions (like Decimal.Divide())?

+17  A: 

They are the same. The type decimal is an alias for System.Decimal.

So basically decimal is the same thing as Decimal. It's down to user's preference which one to use but most prefer to use int and string as they are easier to type and more familiar among C++ programmers.

Martin
If they are exactly the same, why do we have both of them? Why not just one of them?
Matthew Jones
decimal is the C# specific version of the .NET type System.Decimal -- as it turns out its only an alias.
Nate Bross
Great! Thanks guys!
Matthew Jones
RolandTumble
@RolandTumble - Why is a case-sensitivity "boneheaded" in your opinion?
Andrew Hare
A: 

decimal, int, string are all just short hand notation to make things easier/prettier for you. The framework doesn't really know what a "decimal" is, but it does know System.Decimal, so when you compile your code, decimal just turns into System.Decimal. Try looking at some code where all the types are fully qualified, then try looking at some code where the aliases are used, I think most programmers will prefer the more compact aliases and perceive it as being easier to read. I also think it might be a throw back to C/C++ to make transitioning easier.

Bob
A: 

The built-in C# types aren't all structs*. They are aliases for the predefined types in the System namespace. They are literally the same in all ways except formatting. The alias types are lowercase and formatted like keywords (dark blue). The System types are PascalCased and formatted like types (light blue).


*object and string are classes

Anthony Mastrean
The built-in C# types (int, double, and so on) _are_ structs.
Eric Lippert
Thanks Eric, I left my brain at home for that one.
Anthony Mastrean