‘Stupid’ Question 31: What's the deal with String and string in C#?
To String or not to string, that is the question
I had to ask. There is ‘String’ and then there is ‘string’. When I did internship I noticed in the legacy code that the previous developers had been very fon of the String usage instead of string. And I was curious what was ‘right’. So, what is the deal with String and string? You know what I found out? That this was one of the most upvoted questions on Stackoverflow!
‘String’ and ‘string’ is the same, string without the uppercase is an alias for the String type. Alias definition: “an alternate name for someone or something”. They are both compiled to System.String in Intermediate Language (low level language that C# code is compiled to). There is no performance difference.
There we go! The answer!
As for when to use it, it is debated. I use option A (and I did see this example used a fair bit on SO)
A) string when you are reffering to an object, String when you are reffering to the class
B) Always use string (the alias)
C) Always use String
D) Randomize it (probably not a good idea ;) )
Last modified on 2012-08-25