Difference between String and string?

while writing program you may get confused sometimes when you are dealing with strings. You may have doubt whether to use string or String. So, in this article am explaining what is the difference between string and String and which is best to use and are there any problems in using both.

There are number of developers who really confused, whether to use string or String in their code. Because in C# the string equal to System.String, there is no difference and either can be used.

In C# the keywords actually are synonyms for their types. So int is equal to System.Int32 ,short is equal to System.Int16 and string is equal to System.String. They have the keywords because they are easier to remember and programmers coming from other languages like c/c++ would also be familiar with these types.

System.String is the .Net string class. Where, string type represents a string of Unicode characters.It is an alias for System.String. So in the technical point of view, there is no difference between string and String. It is just like int vs. System.Int32.

But it is generally recommended to use string any time you are referring to an object. e.g.

string name = "JohnSmith";

And, It is generally recommended to use String if you need to refer specifically to the class. e.g.

string wish = String.Format("Good Morning {0}!", name);

This is the style that Microsoft uses in their examples. for examples refer this : http://msdn.microsoft.com/en-us/library/fht0f5be.aspx#ctl00_rs1_mainContentContainer_ctl110CSharp

Personally, I prefer to use c# aliases.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: