Using C# and .NET 3.5, what's the best way to handle this situation. I have hundreds of fields to compare from various sources (mostly strings). Sometimes the source returns the string field as null and sometimes as empty. And of course, sometimes there is text in the fields. My current comparison of strA != strB isn't cutting it because strA is null and strB is "", for example. I know I could do the string.IsNullOrEmpty which results in a double comparison and some ugliness. Is there a better way to handle this? I thought extension methods, but you can't extend operators.
I guess I'm looking for a sexy way to do this.
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.
They are not the same thing and should be used in different ways. null should be used to indicate the absence of data, string. Empty (or "" ) to indicate the presence of data, in fact some empty text.
The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.
Doesn't eliminate the extra underlying comparisons, but for the sexiness factor, you could use something like this:
(strA ?? "") == (strB ?? "")
or the slightly less sexy, but preferable form:
(strA ?? string.Empty) == (strB ?? string.Empty)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With