Lance Fisher Lance Fisher - 1 year ago 44
C# Question

What is the difference between String and string in C#?

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?

Answer Source

string is an alias in C# for System.String. So technically, there is no difference. It's like int vs. System.Int32.

But int automatically references System.Int32 because it is the default, while there are many other integer(int) types. Using int just defaults to the 32 bit integer if you don´t explicitly tell in your code what type of integer it should expect. If you reference Int16 instead of int, you will specify the type from the default Int32.

As far as guidelines, I think it's generally recommended to use string any time you're referring to an object.


string place = "world";

Likewise, I think it's generally recommended to use String if you need to refer specifically to the class.


string greet = String.Format("Hello {0}!", place);

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.