What’s the difference between String and string?

In C#, what is the difference between String and string? (note the case)

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

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

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);

Here are a few noteworthy points about both:

  • string is just an alias for System.String. The compiler will treat them identically.
  • string is a reserved word, but String is just a class name. This means that string cannot be used as a variable name by itself.
  • You can’t use String without using System; beforehand.
  • string is a type in C#. System.String is a type in the CLR.
  • You can’t use string in reflection; you must use String.

You can do more localized aliasing for types and namespaces with the using keyword. e.g.

using str = System.String;
str s = "Now you've got another alias for string!";

String and Other Aliases

string is an alias for System.String. They compile to the same code, so at execution time there is no difference whatsoever. This is just one of the aliases in C#. The complete list is:

object:  System.Object
string:  System.String
bool:    System.Boolean
byte:    System.Byte
sbyte:   System.SByte
short:   System.Int16
ushort:  System.UInt16
int:     System.Int32
uint:    System.UInt32
long:    System.Int64
ulong:   System.UInt64
float:   System.Single
double:  System.Double
decimal: System.Decimal
char:    System.Char

Vivek Maskara

I am pursuing BTech in Software Engineering from Delhi Technological University. I develop apps, create websites and blog about my app experiences.

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding