20

As regards best practices, is there a meaningful difference between using:

Double d;

and

double d;

I know best practices are fraught with contradictions, so I know the answers may vary here. I just want to know the pragmatic difference between the two.

Dinah
  • 52,922
  • 30
  • 133
  • 149
IamIC
  • 17,747
  • 20
  • 91
  • 154
  • 3
    See also http://stackoverflow.com/questions/1947068/in-c-are-decimal-and-decimal-different and http://stackoverflow.com/questions/215255/string-vs-string-in-c and http://stackoverflow.com/questions/915938/difference-between-decimal-and-decimal and http://stackoverflow.com/questions/1017282/c-difference-between-system-object-and-object. They're not *exact* duplicates, but they might as well be. – LukeH Oct 16 '10 at 23:27

5 Answers5

23

There is no difference. double is just an alias for System.Double in C#.

Note that VB.NET doesn't have the same aliasing (int for System.Int32, double for System.Double, etc), so the aliasing is just applicable to C#, not .NET as a whole.

kuszi
  • 2,069
  • 29
  • 36
Adam Lear
  • 38,111
  • 12
  • 81
  • 101
  • So I take it that if one wanted to stick to using System types, Int32 would be preferred to int. – IamIC Oct 16 '10 at 23:25
  • 1
    @IanC: yes, but I don't know why one would want to stick to System types. There's no particular advantage to doing so. – Adam Lear Oct 16 '10 at 23:27
  • @Adam I suppose purely from the viewpoint that it's the base and the types are sometimes more descriptive (Int32 vs. int). – IamIC Oct 16 '10 at 23:28
  • @IanC: I personally prefer 'int' and 'long', and the way Visual Studio does syntax highlighting makes those base value types (and string :)) easier to see, since the aliases don't look like normal classes. – Adam Lear Oct 16 '10 at 23:31
  • @Adam lol you make a good point. Like I hinted at in my post, I just spent about 4 hours reading best practices. There wasn't much that was agreed on... so much for the concept. – IamIC Oct 16 '10 at 23:34
  • This is wrong. `double` is not an alias for `Double`, it is an alias for `System.Double`. Which means that there is a very important difference between `double` and `Double`: `double` will *always* refer to `System.Double`, `Double` will refer to whichever `Double` happens to be in scope. Also, I believe that `double` is not part of ECMA C# or ISO C#, only Microsoft Visual C#, so it is not guaranteed to work cross-platform. – Jörg W Mittag Oct 17 '10 at 00:04
  • @Jorg: LukeH said what I was going to say. – Adam Lear Oct 17 '10 at 01:54
  • `(Double)-1` interpreted as subtraction and thus produce compile error. While `(double)-1` interpreted as cast. – user4003407 Dec 02 '15 at 10:56
  • When disassembling Double, I found code like `[__DynamicallyInvokable]public const double NegativeInfinity = double.NegativeInfinity;`, and this is what has brought me here to this question. This means they cannot be the same, can they? – tethered.sun Aug 29 '17 at 13:27
11

No, there's no difference: double is a C# keyword that's an alias for the System.Double type.

LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 1
    This is wrong. There is a very important difference between `double` and `Double`: `double` will *always* refer to `System.Double`, `Double` will refer to whichever `Double` happens to be in scope. Also, I believe that `double` is not part of ECMA C# or ISO C#, only Microsoft Visual C#, so it is not guaranteed to work cross-platform. – Jörg W Mittag Oct 17 '10 at 00:07
  • 4
    @Jörg: `double` is very much part of ECMA/ISO C# and *is* guaranteed to work the same on any spec-compliant implementation. As for `Double` referring to whichever `Double` is in scope: that goes without saying; note that I was careful to specifically say `System.Double` in my answer. (And anybody who declares their own `Double` type -- or `Int32` or `DateTime` or `Object` etc -- in a more local namespace should be prepared to handle the pitfalls.) – LukeH Oct 17 '10 at 00:23
1

Actually... after disassemble I found out that System.Double is a wrapper around double... So double is not a "shortcut" to System.Double.

1

a bit difference here.

-You can have class,parameter,var named "Double" but if u want to name it "double" u must type @double.

-You can use Qualifier for Double but not for double. ex

System.Double d; //Correct
System.double d; //incorrect. Compiler error: Identifier expected for "System." .
Double Double,@double;//Correct
double double;//incorrect ,Identifier expected for both doubles

the same thing for classes and parameters....also the same for String-string,Boolean-bool .....

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0

In C# the Double and double are the same, however Double guranateed to be the same across all .NET platforms, and as far as I remember the double IS the same on all platform, but is not guaranteed as such (despite being it de facto).

dkguru
  • 142
  • 1
  • 7
    This is wrong. `double` is not an alias for `Double`, it is an alias for `System.Double`. Which means that there is a very important difference between `double` and `Double`: `double` will *always* refer to `System.Double`, `Double` will refer to whichever `Double` happens to be in scope. – Jörg W Mittag Oct 17 '10 at 00:05