Possible Duplicate:
String vs string in C#
I see there are 2 different keywords (classes ?) one which starts with capital S and other with small in c#.
String and string
What is the reason ? is one not sufficient ?
string is a keyword and alias of System.String class, System.String is a class. Also there's more aliases than string:
object: System.Object
string: System.String
bool: System.Boolean
byte: System.Byte
sbyte: System.SByte
short: System.Int16
ushort: System.UInt16
int: System.Int32
uint: System.UInt32
long: System.Int64
ulong: System.UInt64
float: System.Single
double: System.Double
decimal: System.Decimal
char: System.Char
The best answer comes from Jeffrey Richter in his book CLR Via C#. Here are his 3 reasons:
I've seen a number of developers confused, not knowing whether to use string or String in their code. Because in C# the string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used.
In C#, long maps to System.Int64, but in a different programming language, long could map to an Int16 or Int32. In fact, C++/CLI does in fact treat long as an Int32. Someone reading source code in one language could easily misinterpret the code's intention if he or she were used to programming in a different programming language. In fact, most languages won't even treat long as a keyword and won't compile code that uses it.
The FCL has many methods that have type names as part of their method names. For example, the BinaryReader type offers methods such as ReadBoolean, ReadInt32, ReadSingle, and so on, and the System.Convert type offers methods such as ToBoolean, ToInt32, ToSingle, and so on.
string is a C# language alias for the actual class System.String.
You can safely use either. I've seen most people prefer string. If I'm working on an API I'll typically use System.String instead although both are safe.
There's plenty of other aliases, too. For example int is an alias for System.Int32, bool for System.Boolean. The string example just happens to be a casing difference so this question comes up a lot.
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