foote

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


 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, 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.

e.g.


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


As others have noted, string is an alias for System.String. Assuming your code using String compiles to System.String (i.e. you haven't got a using directive for some other namespace with a different String type), 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 

What is the difference between String and string in C#? What is the difference between String and string in C#? Reviewed by Udochi V.C on January 02, 2022 Rating: 5

No comments:

Powered by Blogger.