user27414
user27414

Reputation:

In c# are Decimal and decimal different?

In c# if I use decimal (lower case 'd'), the IDE shows it in dark blue (like int). If I use Decimal (upper case 'd'), the IDE shows it in teal (like a class name). In both cases the tooltip is struct System.Decimal.

Is there any difference? Is one "preferred"?

Upvotes: 9

Views: 1653

Answers (9)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

decimal is a keyword and always refers to the struct System.Decimal defined in base class library. Decimal usually means the same thing as most people have using System; on top of their source code. However, strictly speaking, those are not identical (you can't blindly find-and-replace them):

 namespace Test {
    class Decimal { }
 }

 // File2.cs
 using Test;
 class DecimalIsNotdecimal {
      void Method(decimal d) { ... }
      void Method(Decimal d) { ... } // works!
 }

Upvotes: 1

Bhaskar
Bhaskar

Reputation: 10681

Its the same thing. 'deciaml' is the c# keyword for System.Decimal.

This applied to other types like string and String etc.

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827286

System.Decimal it's a .NET Framework type, decimal is an alias in the C# language.

Both of them are compiled to System.Decimal in IL, so there is no difference.

Is exactly the same thing about int and System.Int32, string and System.String etc..

Upvotes: 2

Joey
Joey

Reputation: 354466

The dark blue is for language keywords, teal is for types. In the case of decimal it's a keyword representing a type alias.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062745

Nope; identical. decimal is defined as an alias to System.Decimal, and is generally preferred, except in public method names, where you should use the proper name (ReadDecimal, etc) - because your callers might not be C#. This is more noticeable in int vs ReadInt32, etc.

There certainly isn't the Java-style boxing difference.

(of course, if you do something like declare a more-namespace-local Decimal that does something completely different, then there are differences, but that would be silly).

Upvotes: 18

Tommy Carlier
Tommy Carlier

Reputation: 8149

No, they're the same thing. One is colored as a keyword (decimal), the other is colored as a type (Decimal). The keyword is just an alias for the type.

Upvotes: 0

Anemoia
Anemoia

Reputation: 8116

No. They are just shortcuts.

If you hover over decimal you'll see System.Decimal. As with int and System.Int32, object and System.Object, string and System.String, double and System.Double

I prefer the decimal and string, but I really think its personal

Upvotes: 0

Jeff Cyr
Jeff Cyr

Reputation: 4864

It is the same thing, Decimal is the .Net type and decimal is the c# keyword.

Upvotes: 2

Neil N
Neil N

Reputation: 25258

decimal is an alias to Decimal. They are the same thing.

Upvotes: 1

Related Questions