String.Empty or null: which is better to return for the Error field from the service?

We all know (more or less) about the difference between String.Empty and null. I still haven't decided what is best for me in the “normal” development (I mean the development of the server side of the ASP.NET application, the WinForms application is all that is C # .NET code), but I saw and read a lot of articles about .

But I did not see a comparison that is better to use when you return string values ​​from services (ASP.NET WebService or WCF service).

What is your suggestion, I should return null or String.Empty:

  • When I need to return the message “Error” (empty or empty will mean “no errors”);
  • When I need to return the value of the field "Email" (zero or empty will mean that "letter is not specified").

Any thoughts are welcome.

+3
source share
5 answers

It is all a matter of taste, style and consistency. Do you have other services that return null / empty already? If so, it is best to compare them. Otherwise, in my opinion, zero is better than empty. As a null value, it is best evaluated. An empty string can be thought of as an error, but no errors were returned.

+3
source

, , , . , , ; , , null, , , ( /).

+1

string.Empty null.

, , NullReferenceException, , .

.

0

NULL string.empty . XSD nillable = "true" .

, , "", .

0

Null undefined. , , , , . , Null . , , .

With the error message, I will again say that Null represents undefined, for example. error not reported. An empty line may indicate an error, but for some reason the part was not provided.

If you agree on your service, it should still be fine.

0
source

Source: https://habr.com/ru/post/1784557/


All Articles