首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 與 VB.NET 型別系統整理(五):可為 Null 的實值型別與 DBNull

     編輯:關於C語言

可為 Null 的實值型別

允許實值型別變數指派 null (C#) / Nothing (VB)。

// C#
Nullable<int> i = null;
int? j = null;

// VB.NET
Dim i As Nullable(Of Integer) = Nothing
Dim j? As Integer = Nothing ' VS2005 不支援

判斷是否有值:HasValue 屬性;取值:Value 屬性或 GetValueOrDefault()。

C# ?? 運算子

int? x = null;
int y = x ?? 5; // x 不為 null 取 x,否則 5

DBNull 與 null/Nothing 差異

// C#
Console.WriteLine(DBNull.Value == null); // False
object item = row[0];
if (item != DBNull.Value) ... // 正確檢查資料庫空值

// VB.NET
Console.WriteLine(DBNull.Value Is Nothing) ' False
Dim item As Object = row(0)
If item = DBNull.Value Then ...

資料庫欄位無值時回傳 DBNull.Value,非 null/Nothing。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved