我有以下方法:float myMethod(MyObject[][] myList) { float a = 0; if (myListProcessingMethod(myList?.Where(x => x.mySatisfiedCondition()).ToList())) { a = 5; } return a;}bool myListProcessingMethod(List<MyObject[]> myList){ bool isSuccess = false; if (myList.Any()) { isSuccess = true; } return isSuccess;}我認為這種情況:if (myListProcessingMethod(myList?.Where(x => x.mySatisfiedCondition()).ToList()))我將條件重構為:if (myList?.Length != 0){ if (myListProcessingMethod(myList.Where(x => x.mySatisfiedCondition()).ToList())) { a = 5; }}這兩個條件是否相等?用傳統(tǒng)方式與第一個NullConditionOperator等效的條件是什么?使用NullConditionalOperator進行第二次傳統(tǒng)檢查的等效條件是什么?
1 回答

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
下面的語句可能會崩潰。如果myList為null,myList?.Length則將為null,并且myList?.Length != 0將為true。這意味著myList.Where可能會因空引用異常而崩潰。
if (myList?.Length != 0)
{
if (myListProcessingMethod(myList.Where(x => x.mySatisfiedCondition()).ToList()))
{
a = 5;
}
}
你可能想要
if (myList?.Length > 0)
...
僅當列表不為null且其Length大于0時,它的評估結果才為true。
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報
0/150
提交
取消