2016-09-21 43 views
0

我得到了一行代碼,則不會進行編譯,紅色波浪狀的下劃線:if(model.Building.Address == null)不工作,不編譯 - 爲什麼?

enter image description here

但是,如果我用model.Building.Address.Equals(空),沒有任何問題:

enter image description here

實際的錯誤信息是:

enter image description here

和地址定義這樣一個普通的屬性:

enter image description here

enter image description here

問:爲什麼檢查(model.Building.Address == NULL)不會編譯???

+0

你能和我們分享這個錯誤嗎? –

+0

這裏的地址類型是什麼?它是一個'struct'嗎? – DavidG

+0

添加更多信息,請重新加載 – monstro

回答

1

如果model.Building.Address == null不那麼編譯肯定,這是一個結構(我的意思是Address是爲非可空類型,這是一個struct實際上)

+0

是真的,這是一個結構,改爲class,謝謝 – monstro

8

的比較null可能無法正常工作的原因是,model.Building.Address是非空值類型,即struct。這種值類型不能是null,所以編譯器會發出錯誤。

比較model.Building.Address.Equals(null)會編譯,但它根本沒有比較,因爲它總是返回false

兩個辦法讓它工作正在改變用於Addressstructclass類型,或由它的類型名稱後附加一個問號?使Address空。

+0

確切的情況,它是一個結構。更改爲類。 – monstro