Difference between String and string in C#

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 Tweet

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.

Difference between String and string in C#

As per the guidelines, I think it’s generally recommended to use string any time you’re referring to an object.

Example:

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

Example:

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!

  • Another point to remember is the “string” is an alias for “System.String”, which means you can use “string” without having a “using System;” directive, while saying “String” without the directive will fail. Granted, one almost never has a C# source file without “using System;” but it’s something to keep in mind.

  • Daniele Rota Nodari

    Due to syntax coloring and especially due to names being more descriptive about the primitive types binary meaning, I use class names only for parameters, fields and properties whose type I want to highlight and distinguish from other ones or generally emphasize, like different method overloads.

  • dennis_robert_walter

    Use string for instances of a string object.

    Use String for accessing static members of the string class.

  • SuperTroll

    var is evil. If the data type can be known at design time it should be declared for code clarity. I know most developers today want to use var, but it’s lazy coding.

    • David Arno

      This ought to be very funny. However, I’ve recently discovered that there are clueless devs, who ought to give up and become McD crew, who really believe this stuff! Unbelievable, I know, but apparently they really exist! 😀 😀 😀