Skip to main content

Null coalescing operation can be used

In This Article

CodeRush Classic shows the Null coalescing operation can be used code issue if a ternary expression can be expressed as a null-coalescing operation.

#Fix

Convert the ternary expression to a null-coalescing operation.

#Purpose

Highlights ternary expressions, which can be expressed as a null-coalescing operation to improve code readability.

#Example

private string MyString;
public string GetMyString()
{
    return (MyString == null ? "no value" : MyString);
}

Fix:

private string MyString;
public string GetMyString()
{
    return (MyString ?? "no value");
}