Difference between String and string in C#
Contents
Difference between String and string in C#
Hello Readers,
In this post, I will explain about the Difference between String and string in C# with help of simple code snippet or one liner code.
Actually, string is an alias in C# for System.String. So technically, there is no difference. It’s like int versus System.Int32.
Difference between String and string in C# #difference #string #csharp #microsoft Click To TweetBut 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 per the guidelines, I think it’s generally recommended to use string
any time you’re referring to an object.
Example:
1 |
string place = "world"; |
Likewise, I think it’s generally recommended to use String
if you need to refer specifically to the class.
Example:
1 |
string greet = String.Format("Hello {0}!", place); |
This is the style that Microsoft tends to use in their examples.
Take away points
- Use
string
while referring to an object. - Use
String
while referring to the class.
Hope you got the difference between String and string in c#.
What do you think?
Dear Reader,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.
Happy Reading!