Back to Devexpress

Compress to/Expand Null Coalescing Operation

coderushforroslyn-115402-refactoring-assistance-compress-to-expand-null-coalescing-operation.md

latest1.4 KB
Original Source

Compress to/Expand Null Coalescing Operation

  • Aug 03, 2020
  • 2 minutes to read

Purpose

Converts a ternary expression to an equivalent null coalescing operation and vice versa. Use of the null coalescing operations is the most compact way to specify null expressions in C#.

Availability

  • The Compress to Null Coalescing Operation Refactoring is available when the caret is inside a “ ? : “ operator (short form of if ). The operator should compare a variable to null. The same variable should be one of two alternative results.
  • The Expand Null Coalescing Operation is available when the caret is on a statement that uses the ?? operator.

Usage

  1. Place the caret on a compressible ternary expression.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Compress to Null Coalescing Operation from the menu ( Expand Null Coalescing Operation if you are expanding the null coalescing operation).

After execution, the Refactoring compresses a ternary expression to an equivalent null coalescing operation or expands the null coalescing operation into an equivalent ternary expression.

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

See Also

Compress to/Expand Ternary Expression