Okay, this may be a dumb question, but I've not been able to find any information on it.
Are String.Empty and string.Empty the same? I always find myself gravitating towards using the upper case version (String.Empty) because I prefer the color and look of it in my IDE than the lower case version (string.Empty)...
Is there a "correct" way to use these that differ or is it entirely down to personal preference? It was my assumption that they're both the same, but to be honest, I never gave it any thought until for whatever reason today I wondered "If they both exist, they must both exist for a reason".
Is there a reason that anyone knows of? If so, what is it? Can anyone enlighten me?
P.S. The "exact duplicates" only answer half of the question - "which is right?", not the "why do they both exist?"
Exact Duplicate: http://stackoverflow.com/questions/7074/in-c-what-is-the-difference-between-string-and-string
Exact Duplicate: http://stackoverflow.com/questions/215255/string-vs-string-in-c
-
In C#, lower-case type names are aliases for the
System.xxx
type names, e.g.string
equalsSystem.String
andint
equalsSystem.Int32
.It's best practice to use these language alieses for the type names instead of their framework equivalent, for the sake of consistency. So you're doing it wrong. ;-)
As for a reason why they both exist, the .NET types exist because they are defined in a language-independet standard for the .NET libraries called CTS (common type system). Why C# defines these aliases is beyond me (VB does something quite similar). I guess the two reasons are
- Habit. Get all these C and Java programmers to use C# by providing the same type names for some fundamental types.
- Laziness: You don't have to import the
System
namespace to use them.
EDIT Since many people seem to prefer the other notation let me point out that this is by no means unreasonable. A good case can actually be made for the usage of the CTS type names rather than C#'s keywords and some superficially good arguments are offered in the other answers. From a purity/style point of view I would probaly concur.
However, consider if this is worth breaking a well-established convention that helps to unify code across projects.
Arjan Einbu : No, he is " gravitating towards using the upper case version (String.Empty)", thus doing it wrong according to your saying...Software Monkey : Did you mean to say it's best practices *NOT* to use these aliases?Ed Swangren : Also, I would rather not type Int32 instead of int.Joel Mueller : I tend to use the alias when declaring the type, and the actual name when calling a static member. So int foo, String.Empty and Int32.Parse for me.Konrad Rudolph : Damn, I misread this part of the question. Yes, indeed, the common practice dictates the use of the lowercase variants.Michael Prewecki : It's also Microsofts recommended programming practices to use aliased versions of the data types.Ed Swangren : Hmmm, I didn't know that. I use the class names because I like the coloring, as int does not look like a class, and we are calling a static method. -
It is conceptually similar to something like this:
using int=System.Int32
Frederik Gheysels : hmm, looks like i was too slow :) -
string is mapped to the String class AFAIK, so they're the same.
The same is true for, for example int and Int32.
-
They are both the same.
Personally I prefer using the lowercase string, the "blue one", using the C# keyword instead of the .NET class name for the same reason I'm using int instead of Int32. Also, the lowercased one doesn't require inclusion of the System namespace...
-
Personally, I prefer to use String as both String and Object are references whereas all the other base types are value types. In my mind, that's the clearest separation.
0 comments:
Post a Comment