In C#, what is the difference between String
and string
? (note the case)
string
is an alias in C# for System.String
. So technically, there is no difference. It’s like int
vs. System.Int32
.
As far as guidelines, I think it’s generally recommended to use string any time you’re referring to an object. e.g.
string place = "world";
Likewise, I think it’s generally recommended to use String if you need to refer specifically to the class.
e.g.
string greet = String.Format("Hello {0}!", place);
Here are a few noteworthy points about both:
string
is just an alias forSystem.String
. The compiler will treat them identically.string
is a reserved word, butString
is just a class name. This means thatstring
cannot be used as a variable name by itself.- You can’t use
String
withoutusing System;
beforehand. string
is a type in C#.System.String
is a type in the CLR.- You can’t use
string
in reflection; you must useString
.
You can do more localized aliasing for types and namespaces with the using keyword. e.g.
using str = System.String;
//...
str s = "Now you've got another alias for string!";
String and Other Aliases
string
is an alias for System.String
. They compile to the same code, so at execution time there is no difference whatsoever. This is just one of the aliases in C#. The complete list is:
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
Recent Comments