问题
With the new C# 8 capabilities is there a short cut now for this code structure:
if (App.selectedPhrases == null)
App.selectedPhrases = App.DB.GetSelectedPhrases();
回答1:
Yes, it is called 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.
回答2:
App.selectedPhrases ??= App.DB.GetSelectedPhrases();
来源:https://stackoverflow.com/questions/59300159/short-cut-for-a-check-for-null-and-assigning-a-value-to-it-if-it-is