In C#, what is the difference between String
and string
? (see capitals)
Example:
string s = "¡Hola mundo!";
String S = "¡Hola mundo!";
What are the rules of use for each? And what are the differences?
Original question: What's the difference between String and string?
String
is the name of the classSystem.String
string
is the alias ofSystem.String
in C#: string (C# Reference)At execution level there is no difference since the compiler converts both lines into the same thing.
For example this is what LINQPad generates with the following two statements:
As you can see both lines generate the instruction:
In the same way, for example, when invoking one of its methods
Both lines refer to the same class
System.String
and generate the same codeEffectively,
string
it's an alias of System.String . Microsoft does not favor either Microsoft MSDN C# string nomenclature in its documentation .I remember seeing an MSDN Channel 9 video where they recommended
string
for local variables, and String especially for parameters and return types. Unfortunately, I am unable to find the video right now. And besides, it seems to me an unnecessary distinction and one that will probably be very difficult to carry out.string in C# is an alias of the .NET Framework Type (CLR type) System.String
(Idem for example to System.Int32 & int )
You can use it as an alias, but sometimes in organizations coding policies are used to, for example, use the class (and not the alias) in the constructor, no matter how much you can use it.
Example:
But for example you can't create string aliases. Example of aliases:
To be used for better code compression
string
is an alias inC#
forSystem.String
, in its documentation Microsoft uses itstring
in its examples.