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: What is the difference between String and string in C#?
Exact Duplicate: String vs string in C#
The toupper() function in C++ converts a given character to uppercase. It is defined in the cctype header file.
Convert a Character to Uppercase/Lowercase Using the toUpperCase() / toLowerCase() Method.
In C#, the Toupper() function of the char class converts a character into uppercase. In the case that we will be discussing, only the first character of the string needs to be converted to uppercase; the rest of the string will stay as it is.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With