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?
String
is not a keyword and it can be used as Identifier whereasstring
is a keyword and cannot be used as Identifier. And in function point of view both are same.String
stands forSystem.String
and it is a .NET Framework type.string
is an alias in the C# language forSystem.String
. Both of them are compiled toSystem.String
in IL (Intermediate Language), so there is no difference. Choose what you like and use that. If you code in C#, I'd preferstring
as it's a C# type alias and well-known by C# programmers.I can say the same about (
int
,System.Int32
) etc..It's a matter of convention, really.
string
just looks more like C/C++ style. The general convention is to use whatever shortcuts your chosen language has provided (int/Int forInt32
). This goes for "object" anddecimal
as well.Theoretically this could help to port code into some future 64-bit standard in which "int" might mean
Int64
, but that's not the point, and I would expect any upgrade wizard to change anyint
references toInt32
anyway just to be safe.New answer after 6 years and 5 months (procrastination).
While
string
is a reserved C# keyword that always has a fixed meaning,String
is just an ordinary identifier which could refer to anything. Depending on members of the current type, the current namespace and the appliedusing
directives and their placement,String
could be a value or a type distinct fromglobal::System.String
.I shall provide two examples where
using
directives will not help.First, when
String
is a value of the current type (or a local variable):The above will not compile because
IEnumerable<>
does not have a non-static member calledFormat
, and no extension methods apply. In the above case, it may still be possible to useString
in other contexts where a type is the only possibility syntactically. For exampleString local = "Hi mum!";
could be OK (depending on namespace andusing
directives).Worse: Saying
String.Concat(someSequence)
will likely (depending onusing
s) go to the Linq extension methodEnumerable.Concat
. It will not go to the static methodstring.Concat
.Secondly, when
String
is another type, nested inside the current type:Neither statement in the
Example
method compiles. HereString
is always a piano string,MyPiano.String
. No member (static
or not)Format
exists on it (or is inherited from its base class). And the value"Goodbye"
cannot be converted into it.string
is an alias in C# forSystem.String
.So technically, there is no difference. It's like
int
vs.System.Int32
.As far as guidelines, it's generally recommended to use
string
any time you're referring to an object.e.g.
Likewise, I think it's generally recommended to use
String
if you need to refer specifically to the class.e.g.
This is the style that Microsoft tends to use in their examples.
It appears that the guidance in this area may have changed, as StyleCop now enforces the use of the C# specific aliases.
string
is a reserved word, butString
is just a class name. This means thatstring
cannot be used as a variable name by itself.If for some reason you wanted a variable called string, you'd see only the first of these compiles:
If you really want a variable name called string you can use
@
as a prefix:Another critical difference: Stack Overflow highlights them differently.