coderushforroslyn-115431-refactoring-assistance-compress-to-expand-ternary-expression.md
Converts an if-else conditional into an expression that uses a ternary operator (“ ? : “ operator in C#, inline IIf function in Visual Basic) and vice versa. These refactorings also support ref ternary expressions. Ternary expressions reduce the amount of code.
Place the caret on a compressible if-else expression.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
Select Compress to Ternary Expression from the menu ( Expand Ternary Expression if you are expanding the ternary expression).
After execution, the Refactoring compresses an if-else conditional into an equivalent ternary expression or expands the ternary expression into an equivalent if-else conditional.
public ref int GetValue(int[] arr1, int[] arr2) {
return ref (arr1 != null ? ref arr1[0] : ref arr2[0]);
}
b = If(a, 10, 20)
See Also
Compress to/Expand Null Coalescing Operation