0

can any one explain me what a difference between Decimal as Struct declaration and decimal as variable type in C#?

If they are the same things, so why both of them exists in C#.

The similar hapen with Bool and bool, String and string... etc...

nicael
  • 16,503
  • 12
  • 50
  • 80
Anton Selin
  • 2,832
  • 5
  • 16
  • 23

4 Answers4

6

The type is global::System.Decimal; decimal is just an alias to that. They both exist for the same reason that you use int instead of System.Int32 - although in the case of decimal the advantage is ... less clear - it basically comes down to "doesn't need a using System; directive", but... meh.

They are identical/synonyms (assuming of course that your Decimal resolves to System.Decimal, and not some custom My.Decimal).

Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 2
    At work, this is part of our style-guide: When using static functionality of a class, use the capitalized version (eg. String.Compare). When instantiating a variable, use the lower case version (eg. string path). I find it increases legibility. – SBI Nov 29 '13 at 10:42
  • @SBI I see absolutely no advantage or utility in that, but: if it works for you... – Marc Gravell Nov 29 '13 at 10:43
  • 1
    It's nothing huge, but when scanning code quickly it can save you a second glance. And we don't seem to be the only ones: http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string with even MS using it in their examples: http://msdn.microsoft.com/en-us/library/System.String.Format.aspx#Format1_Example – SBI Nov 29 '13 at 10:44
3

decimal is short hand notation for System.Decimal structure.
so there is no difference between both of them.

If they are the same things, so why both of them exists in C#.

it's for user convenience.so user can type decimal instead of System.Decimal.

The similar hapen with Bool and bool, String and string

Yes similarly there are short hand notations for many types.
few of them are as below:

-Type-        -Short hand notation-

System.Byte   - byte
System.Int16  - short
System.Int32  - int
System.Int64  - long
System.SByte  - sbyte
System.UInt16 - ushort
System.UInt32 - uint
System.UInt64 - ulong


System.Single  - float
System.Double  - double

System.Boolean - bool
System.Char    - char


String         - string
Sudhakar Tillapudi
  • 24,787
  • 5
  • 32
  • 62
0

Decimal is the CLR type and decimal is the c# keyword.

They are both the same thing to your programs and c# creates a Decimal in the background.Actually decimal is just alias of Decimal.

vikky
  • 4,746
  • 4
  • 40
  • 63
0

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.