在 1.1 中
int i = null ; 是無法編譯的
C# 2.0 新增一種型別叫 Nullable 可以允許將 int 設為 null
C# 2.0 新增一種型別叫 Nullable 可以允許將 int 設為 null
int? i =null ; <----- 這個是可以成功編譯的
當然 套用在 DateTime 或者 bool 一樣可以
另外 C# 2.0為了讓Nullable型別和non-Nullable 之間做轉換,提供了一個新的表示法"??"
範例如下
int? a = 1;
int? b = null;
int c = a; // 無法 compile
int c = a ?? 100; // 正確
int d = a + b; // 無法 compile
int d = a + b ?? -1; // 正確
int? a = 1;
int? b = null;
int c = a; // 無法 compile
int c = a ?? 100; // 正確
int d = a + b; // 無法 compile
int d = a + b ?? -1; // 正確
全站熱搜
留言列表