Why do lowercase and uppercase versions of string exist and which should I use?
C#C# Problem Overview
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: https://stackoverflow.com/questions/7074/in-c-what-is-the-difference-between-string-and-string
Exact Duplicate: https://stackoverflow.com/questions/215255/string-vs-string-in-c
C# Solutions
Solution 1 - C#
In C#, lower-case type names are aliases for the System.xxx
type names, e.g. string
equals System.String
and int
equals System.Int32
.
It's best practice to use these language aliases 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-independent 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 probably concur.
However, consider if this is worth breaking a well-established convention that helps to unify code across projects.
Solution 2 - C#
It is conceptually similar to something like this:
using int=System.Int32
Solution 3 - C#
string is mapped to the String class AFAIK, so they're the same.
The same is true for, for example int and Int32.
Solution 4 - C#
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...
Solution 5 - C#
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.