Compress to Null Coalescing Operation (C#)
In This Article
Converts a ternary expression to an equivalent null coalescing operation.
#Availability
Available from the context menus or via shortcuts:
- when the caret is on a ? statement. The statement’s left hand side should compare a variable to null. The same variable should be one of two alternatives in the right-hand side.
#Notes
- This refactoring is the functional opposite of Expand Null Coalescing Operation (C#).
#Example
private string GetMessage(string text)
{
return (│text != null ? text : "null");
}
Result:
private string GetMessage(string text)
{
return (│text ?? "null");
}
#Screenshot
See Also