Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .NET have a way of resolving a null string to String.Empty?

Tags:

c#

.net

I've got a one line method that resolves a null string to string.Empty which I thought might be useful as an extension method - but I can't find a useful way of making it so.

The only way I could see it being useful is as a static method on the string class because obviously it can't be attributed to an instance as the instance is null and this causes a compiler error. [Edit: Compiler error was due to uninitialized variable, which I misinterpreted]

I thought about adding it to a helper class but that just adds unnecessary complexity from a discoverability standpoint.

So this question is in two parts I suppose:

  1. Does the .NET framework have a built in way of resolving a null string to string.Empty that is common knowledge that I have missed somewhere along the way?
  2. If it doesn't - does anyone know of a way to add this method as a static extension of the string class?

Cheers in advance

Edit:

Okay, I guess I should've been a little more clear - I'm already well aware of null coallescing and I was using this in a place where I've got a dozen or so strings being inspected for calculation of a hash code.

As you can imagine, 12 lines of code closely following each other all containing the null coallescing syntax is an eyesore, so I moved the null coallescing operation out to a method to make things easier easier on the eyes. Which is perfect, however, it would be a perfect extension to the string object:

int hashcode =
FirstValue.ResolveNull().GetHashCode() ^
SecondValue.ResolveNull().GetHashCode() ^
...

over a dozen lines is a lot easier to read than:

int hashcode =
(FirstValue ?? String.Empty).GetHashCode() ^
(SecondValue ?? String.Empty).GetHashCode() ^
...

I was running into compiler problems when I didn't explicitly declare my string values as null but relied on the implicit:

 string s;

If however, you explicitly define:

string s = null;

You can quite easily call:

s.ResolveNull();

Thanks all for your input.

like image 835
BenAlabaster Avatar asked Aug 06 '09 20:08

BenAlabaster


4 Answers

I don't think there's anything built in for this. My first thought, and what I do often, is use the coalesce operator:

string s = null;
string x = s ?? string.Empty;
like image 159
Matt Kellogg Avatar answered Oct 19 '22 03:10

Matt Kellogg


The only way I could see it being useful is as a static method on the string class because obviously it can't be attributed to an instance as the instance is null and this would cause a runtime error.

C# 3.0 extension methods can be called on null receivers (since they are static in practice), but behave as instance methods. So just make it an extension method.

like image 45
Pavel Minaev Avatar answered Oct 19 '22 03:10

Pavel Minaev


someExpressionInvolving(s ?? "");

There is no point getting excited about string.Empty vs "" - there is negligible difference:

Console.WriteLine(ReferenceEquals(string.Empty, "")); // true
like image 23
Marc Gravell Avatar answered Oct 19 '22 03:10

Marc Gravell


Extension method:

public static class StringExtensions
{
    public static String EmptyIfNull(this String instance)
    {
        return instance ?? String.Empty;
    }
}

of course, you can just as easily write instance ?? String.Empty in places where you need to use String.Empty instead of null.

like image 3
Lasse V. Karlsen Avatar answered Oct 19 '22 02:10

Lasse V. Karlsen