检查是否为 null 并为其分配值的快捷方式?

Short cut for a check for null and assigning a value to it if it is?

有了新的 C# 8 功能,此代码结构现在有捷径:

if (App.selectedPhrases == null)
    App.selectedPhrases = App.DB.GetSelectedPhrases();
App.selectedPhrases ??= App.DB.GetSelectedPhrases();

是的,叫做Null-coalescing assignment:

App.selectedPhrases ??= App.DB.GetSelectedPhrases();

C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null.