'How do I combine ternary operator with null-coalescing operator in one statement?

Currently I have this code

contract.Quantity = contract.Quantity ?? 0;

and also this code

contract.Quantity = contract.ContractType == "S" ? contract.Quantity * -1 : contract.Quantity;

My question is How do I combine ternary operator with null-coalescing operator in one statement?



Solution 1:[1]

I wouldn't contract them into one statement, clarity is more important than terseness.

I would do:

contract.Quantity ??= 0;
if (contract.ContractType == "S") contract.Quantity * -1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gcores