Is there a shorthand way to denullify a string in C#?

Posted by Moose Factory on Stack Overflow See other posts from Stack Overflow or by Moose Factory
Published on 2010-04-21T10:40:39Z Indexed on 2010/04/21 10:43 UTC
Read the original article Hit count: 185

Filed under:
|
|
|

Is there a shorthand way to denullify a string in C#?

It would be the equivalent of (if 'x' is a string):

string y = x == null ? "" : x;

I guess I'm hoping there's some operator that would work something like:

string y = #x;

Wishful thinking, huh?

The closest I've got so far is an extension method on the string class:

public static string ToNotNull(this string value)
{
    return value == null ? "" : value;
}

which allows me to do:

string y = x.ToNotNull();

Any improvements on that, anyone?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#