We all know (more or less) about difference between String.Empty and null. I still didn’t decided what is better for me in “usual” development (I mean development of server side part of ASP.NET application, WinForms application – everything that is C#.NET code), but I saw and read a lot of articles about.
But I didn’t see comparison what is better to use when you are returning string values from the services (ASP.NET WebService or WCF service).
What is your suggestion should I return null or String.Empty:
- When I need to return ‘Error’ message (null or empty will mean ‘there are no errors’);
- When I need to return ‘Email’ field value (null or empty will mean ’email was not specified).
Any thoughts are welcome.
This is all a matter of taste, style and consistency. Do you have other services that return null/empty already? If so, it’s best to match them. Otherwise in my personal opinion null is better than empty. As null serves as a better sentinel value. An empty string could be perceived as an error did occur but no error details were returned.