Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
3.67/5 (4 votes)
See more:
string versus String!

I have a question (a little embarrassing because I see it so late). What is the difference between String and string? Not in the way that String is a Class and string is a reference type. But rather how to think about them in the term of uses.

I searched and find this piece of gold, but I don't know how to use it very well:

  • Use "String" to refer specifically to the String class.
  • Use "string" when referring to an object of the String class.

How to use them, accordingly?

Thanks again.

Posted
Updated 25-Aug-09 10:36am
v2

String == string. They're aliases of the same class, just like ulong is an alias of UInt64

 
Share this answer
 
There is no difference. string is the C# alias to String (which is really System.String). When the code compiles, all of your string types are converted to System.String in the IL.

Personally, I prefer using string when I am typing a variable and String when I am acessing static members of the class. This is only personal preference as I think it makes the code easier to follow. There is nothing wrong with code like:
bool empty = string.IsNullOrEmpty("");
instead of
bool empty = String.IsNullOrEmpty("");


 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900