I have a single line method that resolves an empty string to string.Empty, which I thought might be useful as an extension method, but I cannot find a useful way to do this.
The only way I could see that this is useful is with the static method in the string class, because obviously it cannot be attributed to the instance since the instance is null and this causes a compiler error. [Edit: A compiler error occurred due to an uninitialized variable that I misinterpreted]
I was thinking of adding it to the helper class, but that just adds unnecessary complexity in terms of openness.
So this question has two parts:
- Is there a built-in way in the .NET Framework to allow an empty string for string.Empty, which is well known that I missed somewhere along the way?
- If this is not the case, does anyone know a way to add this method as a static extension of the string class?
Greetings in advance
Edit:
Well, I think I should have been a little clearer - I already know the zero treat well, and I used it in the place where I have a dozen lines that are checked to calculate the hash code.
As you can imagine, 12 lines of code that are closely related to each other, all that contains zero-coalescent syntax, are an eyesore, so I moved the zero-collision operation to a method to make it easier on the eyes. However, this is an ideal extension for a string object:
int hashcode =
FirstValue.ResolveNull().GetHashCode() ^
SecondValue.ResolveNull().GetHashCode() ^
...
more than a dozen lines are much easier to read than:
int hashcode =
(FirstValue ?? String.Empty).GetHashCode() ^
(SecondValue ?? String.Empty).GetHashCode() ^
...
, null, :
string s;
, , :
string s = null;
:
s.ResolveNull();
.