In C#, I want to initialize a string value with an empty string.
How should I do this? What is the right way, and why?
string willi = string.Empty;
or
string willi = String.Empty;
or
string willi = '';
or what?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use whatever you and your team find the most readable.
Other answers have suggested that a new string is created every time you use
''. This is not true – due to string interning, it will be created either once per assembly or once per AppDomain (or possibly once for the whole process – not sure on that front). This difference is negligible – massively, massively insignificant.Which you find more readable is a different matter, however. It’s subjective and will vary from person to person – so I suggest you find out what most people on your team like, and all go with that for consistency. Personally I find
''easier to read.The argument that
''and' 'are easily mistaken for each other doesn’t really wash with me. Unless you’re using a proportional font (and I haven’t worked with any developers who do) it’s pretty easy to tell the difference.