views:

77

answers:

3

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.

+8  A: 

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

LukeH
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
@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
+3  A: 

There is no difference. double is just an alias for 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.

Anna Lear
So I take it that if one wanted to stick to using System types, Int32 would be preferred to int.
IanC
@IanC: yes, but I don't know why one would want to stick to System types. There's no particular advantage to doing so.
Anna Lear
@Anna I suppose purely from the viewpoint that it's the base and the types are sometimes more descriptive (Int32 vs. int).
IanC
@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.
Anna Lear
@Anna 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.
IanC
VB does alias..
Marc
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
@Jorg: LukeH said what I was going to say.
Anna Lear
A: 

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