我有這個條件空條件運算符是否返回false如果爲空?
if (item?.Value2?.GetType() != typeof(string) && item.get_Value() == 0)
我相信,如果項目是空的?.
操作將返回null,我相信會被解析爲false
導致病情短路和一切都會好起來(item.get_Value()
將不會被調用)
但是我不能肯定,我想也許我需要做的是,像這樣
if (item?.Value2?.GetType() ?? 0 != typeof(string) && item.get_Value() == 0)
但我認爲這可能是矯枉過正,是第一種安全的潛在空引用異常嗎?
沒有必要對「信仰」的編程。你可以隨時啓動編譯器並測試它。 –
是一個單元格Excel範圍項目?你想檢查單元格值是否爲0或顯示的格式文本是否爲「0」?這是矯枉過正,缺少很多邊緣案例。 – Slai
爲什麼不只是1. [閱讀文檔](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators)和/或2.嘗試一下親眼看看? –