class NullableExample { static void Main() { int? num = null; if (num.HasValue == true) { System.Console.WriteLine("num = " + num.Value); } else { System.Console.WriteLine("num = Null"); } //y is set to zero int y = num.GetValueOrDefault(); // num.Value throws an InvalidOperationException if num.HasValue is false try { y = num.Value; } catch (System.InvalidOperationException e) { System.Console.WriteLine(e.Message); } } }以上将显示输出:num = Null Nullable object must have a value. 可空类型具有以下特性:可空类型表示可被赋值为 null 值的值类型变量。无法创建基于引用类型的可空类型。(引用类型已支持 null 值。)。语法 T? 是 System.Nullable<T> 的简写,此处的 T 为值类型。这两种形式可以互换。为可空类型赋值与为一般值类型赋值的方法相同,如 int? x = 10; 或 double? d = 4.108;。如果基础类型的值为 null,请使用 System.Nullable.GetValueOrDefault 属性返回该基础类型所赋的值或默认值,例如 int j = x.GetValueOrDefault();请使用 HasValue 和 Value 只读属性测试是否为空和检索值,例如 if(x.HasValue) j = x.Value;如果此变量包含值,则 HasValue 属性返回 True;或者,如果此变量的值为空,则返回 False。如果已赋值,则 Value 属性返回该值,否则将引发 System.InvalidOperationException。可空类型变量的默认值将 HasValue 设置为 false。未定义 Value。使用 ?? 运算符分配默认值,当前值为空的可空类型被赋值给非空类型时将应用该默认值,如 int? x = null; int y = x ?? -1;。不允许使用嵌套的可空类型。将不编译下面一行:Nullable<Nullable<int>> n;
string是引用类型啊,你这么定义没错?
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
public bool AddProduct(string productName, int? supplierID, int? categoryID, string quantityPerUnit,
decimal? unitPrice, short? unitsInStock, short? unitsOnOrder, short? reorderLevel,
bool discontinued)
{
// Create a new ProductRow instance
NorthwindWithSprocs.ProductsDataTable products = new NorthwindWithSprocs.ProductsDataTable();
NorthwindWithSprocs.ProductsRow product = products.NewProductsRow(); product.ProductName = productName;
if (supplierID == null) product.SetSupplierIDNull(); else product.SupplierID = supplierID.Value;
if (categoryID == null) product.SetCategoryIDNull(); else product.CategoryID = categoryID.Value;
if (quantityPerUnit == null) product.SetQuantityPerUnitNull(); else product.QuantityPerUnit = quantityPerUnit;
if (unitPrice == null) product.SetUnitPriceNull(); else product.UnitPrice = unitPrice.Value;
if (unitsInStock == null) product.SetUnitsInStockNull(); else product.UnitsInStock = unitsInStock.Value;
if (unitsOnOrder == null) product.SetUnitsOnOrderNull(); else product.UnitsOnOrder = unitsOnOrder.Value;
if (reorderLevel == null) product.SetReorderLevelNull(); else product.ReorderLevel = reorderLevel.Value;
product.Discontinued = discontinued; // Add the new product
products.AddProductsRow(product);
int rowsAffected = Adapter.Update(products); // Return true if precisely one row was inserted, otherwise false
return rowsAffected == 1;
}
class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
} //y is set to zero
int y = num.GetValueOrDefault(); // num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}以上将显示输出:num = Null Nullable object must have a value.
可空类型具有以下特性:可空类型表示可被赋值为 null 值的值类型变量。无法创建基于引用类型的可空类型。(引用类型已支持 null 值。)。语法 T? 是 System.Nullable<T> 的简写,此处的 T 为值类型。这两种形式可以互换。为可空类型赋值与为一般值类型赋值的方法相同,如 int? x = 10; 或 double? d = 4.108;。如果基础类型的值为 null,请使用 System.Nullable.GetValueOrDefault 属性返回该基础类型所赋的值或默认值,例如 int j = x.GetValueOrDefault();请使用 HasValue 和 Value 只读属性测试是否为空和检索值,例如 if(x.HasValue) j = x.Value;如果此变量包含值,则 HasValue 属性返回 True;或者,如果此变量的值为空,则返回 False。如果已赋值,则 Value 属性返回该值,否则将引发 System.InvalidOperationException。可空类型变量的默认值将 HasValue 设置为 false。未定义 Value。使用 ?? 运算符分配默认值,当前值为空的可空类型被赋值给非空类型时将应用该默认值,如 int? x = null; int y = x ?? -1;。不允许使用嵌套的可空类型。将不编译下面一行:Nullable<Nullable<int>> n;
今天可是学到东西了!
---------
.NET 2.0以上...所有值类型都有对应的可空类型...因为其实它是泛型T?...见6,7,8楼...
// 或
System.Nullable<string>
// 不成立,因为 string 本身就是可空的。