Although I have intermediate knowledge of asp.net & C#, but I am still not clear with what's the difference between String (capital S) and string in C#?
When to use which one?
Example code:
string name="Pika"; String name ="Pika";
thanks
According to MSDN ,string is an alias for String in the .NET Framework.Despite C# being a case-sensitive language you can see, whether you use “string” or “String,” the outcome is the same.
In simple, there is no difference between string and String. string is a alias created by Microsoft for System.String.
As we go into details, you can see these difference between them:
String
when using the static methods on the String class, like
String.Split()
or String.IsNullOrEmpty()
string
to declare types - variables, properties, return values and parameters. This is consistent with the use of other system types -
int
, bool
, var
etc (although Int32
and
Boolean
are also correct).
"string" keyword is an alias for System.String in the .NET, that means the String and string are equal, you can either one of them while writing code in C#
Syetem.String and string are compiled to System.String in Intermediate Language (IL), so the performance of both is also same, it just depends on your choice, which one to use.
string
while referring to an object. String
while referring to the class. Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly