36

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())?

Matthew Jones
  • 25,644
  • 17
  • 102
  • 155

5 Answers5

53

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
  • 39,309
  • 62
  • 192
  • 278
  • 1
    If they are exactly the same, why do we have both of them? Why not just one of them? – Matthew Jones May 27 '09 at 14:15
  • decimal is the C# specific version of the .NET type System.Decimal -- as it turns out its only an alias. – Nate May 27 '09 at 14:18
  • 3
    If the designers hadn't gone & made the language case-sensitive, there wouldn't be a question. One of the few *truly* boneheaded decisions, IMHO. – RolandTumble May 27 '09 at 14:56
  • 20
    @RolandTumble - Why is a case-sensitivity "boneheaded" in your opinion? – Andrew Hare May 27 '09 at 18:37
  • 3
    Case sensitivity is bone headed because it can lead to mistakes / ambiguity / confusion. Do you really want a DoSomething method and a Dosomething method? Do you really need decimal and Decimal? (playing devils advocate a bit). Equally you don't want developers using case willy nilly - but compiler rules could enforce consistent case. – niico Sep 17 '18 at 15:52
  • There was a case there EFCore code-first was buggy with model's `Decimal` type property ("Access violation" bug) and worked just fined with `decimal`. This was probably because of reflection bug or smth. This means that MS tests covers `decimal` more better... ¯\_(ツ)_/¯ – Vinigas Feb 12 '20 at 14:41
  • @niico I might not want a Dosomething method, but I might like to have a doSomething method that is private. – CodeWarrior Mar 13 '20 at 06:42
  • @niico I wouldn't want a case where one developer used "DoSomething" and another developer used "dosomeThing" and they both referred to the same thing. Case sensitivity ensures that each instances of the name is the same. – David Rice Oct 30 '20 at 14:26
  • @DavidRice You can make it illegal to have both doSomething and DoSomething, forcing one or the other to be used. Allowing both is lunacy. – niico Oct 31 '20 at 17:28
  • @CodeWarrior - just name your private method something else – niico Oct 31 '20 at 17:28
  • @niico I am a big fan of convention for naming as it makes reading and following execution flow easier. I know that doSomething is either a method scoped variable or a private class scoped property or method. Were we not to have case sensitivity, I would be forced to have underscores in my property or method names (yuck). I personally do not see what the big deal is with case. Casing in writing (English at least) helps us to differentiate between usages of words ("Lake Tawakoni" vs "going to the lake"). It seems fairly natural for the paradigm to migrate to code as well. – CodeWarrior Nov 03 '20 at 00:07
6

As C# is a .NET language, all types must map to a .NET Framework Type.

To answer your first question, decimal is an Alias of the System.Decimal .NET Framework type. They may be used interchangeably.

To answer your second question, both Decimal and decimal should extend the same functions, including both from the created variable and from the "Structure" of the value type itself.

decimal FirstDec = 12;
Decimal SecondDec = 13;
decimal ThirdDec = decimal.Ceiling(FirstDec, SecondDec);
Decimal FourthDec = Decimal.Floor(ThirdDec);
bool isEqual = FirstDec.Equals(SecondDec) && FourthDec.Equals(ThirdDec);

The following MSDN Page for Built-In Types will show you which System.ValueType each alias maps to. And for Decimal and decimal specifically, you can reference this MSDN Page for Decimal.

OmniSECT
  • 123
  • 2
  • 7
1

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

Community
  • 1
  • 1
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
0

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
  • 97,670
  • 29
  • 122
  • 130
-1

Hadn't C# been case sensitive, some people would have written applications in all-caps as they did in Cobol and VB. This would have made it impossible for developers to read the code and maintain it. Case-sensitivity is a designer's effort to help making the language more usable.