Something I find myself doing more and more is checking a string for empty (as in \"\"
or null) and a conditional operator.
A current example:
I have a couple of utility extensions that I like to use:
public static string OrDefault(this string str, string @default = default(string))
{
return string.IsNullOrEmpty(str) ? @default : str;
}
public static object OrDefault(this string str, object @default)
{
return string.IsNullOrEmpty(str) ? @default : str;
}
Edit: Inspired by sfsr's answer, I'll be adding this variant to my toolbox from now on:
public static string Coalesce(this string str, params string[] strings)
{
return (new[] {str})
.Concat(strings)
.FirstOrDefault(s => !string.IsNullOrEmpty(s));
}