I love the null-coalescing operator because it makes it easy to assign a default value for nullable types.
int y = x ?? -1;
That\'s great,
Skeet's answer is the best - in particularly I think his ToStringOrNull() is quite elegant and suits your need best. I wanted to add one more option to the list of extension methods:
// Method:
public static object OrNullAsString(this object input, string defaultValue)
{
if (defaultValue == null)
throw new ArgumentNullException("defaultValue");
return input == null ? defaultValue : input;
}
// Example:
var y = Session["key"].OrNullAsString("defaultValue");
Use var for the returned value as it will come back as the original input's type, only as the default string when null