37

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

5 Answers 5

54

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.

Sign up to request clarification or add additional context in comments.

11 Comments

If they are exactly the same, why do we have both of them? Why not just one of them?
decimal is the C# specific version of the .NET type System.Decimal -- as it turns out its only an alias.
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 - Why is a case-sensitivity "boneheaded" in your opinion?
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.
|
7

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.

Comments

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

1 Comment

The built-in C# types (int, double, and so on) are structs.
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.

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.