錯誤訊息:可為 Null 的物件必須具有值         

 

這個錯誤訊息的發生的程式碼寫法,可能是對null物件還想要去取他的Value,或者想把null 轉為非null類型

 

舉例1

DateTime? dt = null;

var v2 = dt.Value;  //這邊會報錯

 

舉例2

int? i = null;

int i2 = (int)i;    //這邊會報錯

 

 

文章標籤
全站熱搜
創作者介紹
創作者 olivermode 的頭像
olivermode

olivermode的部落格

olivermode 發表在 痞客邦 留言(0) 人氣(2,145)