Example (note the case):
string s = "Hello world!";
String s = "Hello world!";
What are the guidelines for the use of each? And what are the differences?
Example (note the case):
string s = "Hello world!";
String s = "Hello world!";
What are the guidelines for the use of each? And what are the differences?
Both are same. But from coding guidelines perspective it's better to use
string
instead ofString
. This is what generally developers use. e.g. instead of usingInt32
we useint
asint
is alias toInt32
FYI “The keyword string is simply an alias for the predefined class
System.String
.” - C# Language Specification 4.2.3 http://msdn2.microsoft.com/En-US/library/aa691153.aspxYes, that's no difference between them, just like the
bool
andBoolean
.It's been covered above; however, you can't use
string
in reflection; you must useString
.Coming late to the party: I use the CLR types 100% of the time (well, except if forced to use the C# type, but I don't remember when the last time that was).
I originally started doing this years ago, as per the CLR books by Ritchie. It made sense to me that all CLR languages ultimately have to be able to support the set of CLR types, so using the CLR types yourself provided clearer, and possibly more "reusable" code.
Now that I've been doing it for years, it's a habit and I like the coloration that VS shows for the CLR types.
The only real downer is that auto-complete uses the C# type, so I end up re-typing automatically generated types to specify the CLR type instead.
Also, now, when I see "int" or "string", it just looks really wrong to me, like I'm looking at 1970's C code.
There is no difference.
The C# keyword
string
maps to the .NET typeSystem.String
- it is an alias that keeps to the naming conventions of the language.Similarly,
int
maps toSystem.Int32
.string is a keyword, and you can't use string as an identifier.
String is not a keyword, and you can use it as an identifier:
Example
The keyword
string
is an alias forSystem.String
aside from the keyword issue, the two are exactly equivalent.