原文地址:http://hi.baidu.com/kamendgp/blog/item/263ff5fa6143cf8c9e5146a9.html
可空类型是 System.Nullable 结构的实例。可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。
可空类型具有以下特性:
可空类型表示可被赋值为 null 值的值类型变量。无法创建基于引用类型的可空类型。(引用类型已支持 null 值。)。
语法 T? 是 System.Nullable<T> 的简写,此处的 T 为值类型。这两种形式可以互换。
为可空类型赋值与为一般值类型赋值的方法相同,如 int? x = 10; 或 double? d = 4.108;。
如果基础类型的值为 null,请使用 System.Nullable.GetValueOrDefault 属性返回该基础类型所赋的值或默认值,例如 int j = x.GetValueOrDefault();( 如果 HasValue 属性为 true,则为 Value 属性的值;否则为当前 Nullable 对象的默认值。默认值的类型为当前 Nullable 对象的类型参数,而默认值的值中只包含二进制零。 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
GetValueOrDefault
1
// This code example demonstrates the
2
// Nullable<T>.GetValueOrDefault methods.
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
using System;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
class Sample
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8
public static void Main()
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
float? mySingle = 12.34f;
11
float? yourSingle = -1.0f;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
Console.WriteLine("*** Display a value or the default value ***\n");
14
// Display the values of mySingle and yourSingle.
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
Display("A1", mySingle, yourSingle);
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
// Assign the value of mySingle to yourSingle, then display the values
19
// of mySingle and yourSingle. The yourSingle variable is assigned the
20
// value 12.34 because mySingle has a value.
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
yourSingle = mySingle.GetValueOrDefault();
23
Display("A2", mySingle, yourSingle);
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
// Assign null (Nothing in Visual Basic) to mySingle, which means no value is
26
// defined for mySingle. Then assign the value of mySingle to yourSingle and
27
// display the values of both variables. The default value of all binary zeroes
28
// is assigned to yourSingle because mySingle has no value.
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
mySingle = null;
31
yourSingle = mySingle.GetValueOrDefault();
32
Display("A3", mySingle, yourSingle);
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
// Reassign the original values of mySingle and yourSingle.
35
mySingle = 12.34f;
36
yourSingle = -1.0f;
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
Console.Write("\n*** Display a value or the ");
39
Console.WriteLine("specified default value ***\n");
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
// Display the values of mySingle and yourSingle.
42
Display("B1", mySingle, yourSingle);
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
// Assign the value of mySingle to yourSingle, then display the values
45
// of mySingle and yourSingle. The yourSingle variable is assigned the
46
// value 12.34 because mySingle has a value.
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
yourSingle = mySingle.GetValueOrDefault(-222.22f);
49
Display("B2", mySingle, yourSingle);
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
// Assign null (Nothing in Visual Basic) to mySingle, which means no value is
52
// defined for mySingle. Then assign the value of mySingle to yourSingle and
53
// display the values of both variables. The specified default value of -333.33
54
// is assigned to yourSingle because mySingle has no value.
55![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
mySingle = null;
57
yourSingle = mySingle.GetValueOrDefault(-333.33f);
58
Display("B3", mySingle, yourSingle);
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
}
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
// Display the values of two nullable of System.Single structures.
63
// The Console.WriteLine method automatically calls the ToString methods of
64
// each input argument to display its values. If no value is defined for a
65
// nullable type, the ToString method for that argument returns the empty
66
// string ("").
67
public static void Display(string title, float? dspMySingle, float? dspYourSingle)
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
Console.WriteLine("{0}) mySingle = [{1}], yourSingle = [{2}]",
70
title, dspMySingle, dspYourSingle);
71
}
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
75
This code example produces the following results:
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
A1) mySingle = [12.34], yourSingle = [-1]
78
A2) mySingle = [12.34], yourSingle = [12.34]
79
A3) mySingle = [], yourSingle = [0]
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
*** Display a value or the specified default value ***
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
B1) mySingle = [12.34], yourSingle = [-1]
84
B2) mySingle = [12.34], yourSingle = [12.34]
85
B3) mySingle = [], yourSingle = [-333.33]
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
*/
88![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
请使用 HasValue 和 Value 只读属性测试是否为空和检索值,例如 if(x.HasValue) j = x.Value;
如果此变量包含值,则 HasValue 属性返回 True;或者,如果此变量的值为空,则返回 False。
如果已赋值,则 Value 属性返回该值,否则将引发 System.InvalidOperationException。
可空类型变量的默认值将 HasValue 设置为 false。未定义 Value。
使用 ?? 运算符分配默认值,当前值为空的可空类型被赋值给非空类型时将应用该默认值,如 int? x = null; int y = x ?? 1 ;
上句中,首先判读x是否为空,若为空,则将1赋值给y。
不允许使用嵌套的可空类型。将不编译下面一行:Nullable<Nullable<int>> n;