Skip to main content

Compress to Null Coalescing Operation (C#)

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

#Example

private string GetMessage(string text)
{
    return (text != null ? text : "null");
}

Result:

private string GetMessage(string text)
{
    return (text ?? "null");
}

#Screenshot

rsCompressToNullCoalescingOperation

See Also