可空类型
可空类型是 System.Nullable 结构的实例。可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。例如,Nullable<Int32>,读作“可空的 Int32”,可以被赋值为 -2147483648 到 2147483647 之间的任意值,也可以被赋值为 null 值。Nullable<bool> 可以被赋值为 true 或 false,或 null。在处理数据库和其他包含可能未赋值的元素的数据类型时,将 null 赋值给数值类型或布尔型的功能特别有用。例如,数据库中的布尔型字段可以存储值 true 或 false,或者,该字段也可以未定义。
{
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 zeroint
y = num.GetValueOrDefault();
// num.Value throws an InvalidOperationException if num.HasValue is falsetry
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;
例子1:
using System;
class NullableBasics
... {
static void DisplayValue(int? num)
...{
if (num.HasValue == true)
...{
Console.WriteLine("num = " + num);
}
else
...{
Console.WriteLine("num = null");
}
// num.Value throws an InvalidOperationException if num.HasValue is false
try
...{
Console.WriteLine("value = {0}", num.Value);
}
catch (InvalidOperationException e)
...{
Console.WriteLine(e.Message);
}
}
static void Main()
...{
DisplayValue(1);
DisplayValue(null);
Console.Read();
}
}
输出结果:
value = 1
num = null
可为空的对象必须具有一个值。
例子2:
using System;
class NullableBoxing
{
static void Main()
{
int? a;
object oa;
// Assigns a to Nullable<int> (value = default(int), hasValue = false).
a = null;
// Assigns oa to null (because x==null), not boxed "int?".
oa = a;
Console.WriteLine("Testing 'a' and 'boxed a' for null...");
// Nullable variables can be compared to null.
if (a == null)
{
Console.WriteLine(" a == null");
}
// Boxed nullable variables can be compared to null
// because boxing a nullable where HasValue==false
// sets the reference to null.
if (oa == null)
...{
Console.WriteLine(" oa == null");
}
Console.WriteLine("Unboxing a nullable type...");
int? b = 10;
object ob = b;
// Boxed nullable types can be unboxed
int? unBoxedB = (int?)ob;
Console.WriteLine(" b={0}, unBoxedB={0}", b, unBoxedB);
// Unboxing a nullable type set to null works if
// unboxed into a nullable type.
int? unBoxedA = (int?)oa;
if (oa == null && unBoxedA == null)
...{
Console.WriteLine(" a and unBoxedA are null");
}
Console.WriteLine("Attempting to unbox into non-nullable type...");
// Unboxing a nullable type set to null throws an
// exception if unboxed into a non-nullable type.
try
...{
int unBoxedA2 = (int)oa;
}
catch (Exception e)
...{
Console.WriteLine(" {0}", e.Message);
}
Console.Read();
}
}
输出结果为:
a == null
oa == null
Unboxing a nullable type...
b = 10 , unBoxedB = 10
a and unBoxedA are null
Attempting to unbox into non - nullable type...
未将对象引用设置到对象的实例。
例子3:
class NullableOperator
... {
static int? GetNullableInt()
...{
return null;
}
static string GetStringValue()
...{
return null;
}
static void Main()
...{
// ?? operator example.
int? x = null;
// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;
Console.WriteLine("y == " + y);
// Assign i to return value of method, unless
// return value is null, in which case assign
// default value of int to i.
int i = GetNullableInt() ?? default(int);
Console.WriteLine("i == " + i);
// ?? also works with reference types.
string s = GetStringValue();
// Display contents of s, unless s is null,
// in which case display "Unspecified".
Console.WriteLine("s = {0}", s ?? "null");
Console.Read();
}
}
i == 0
s = null
基于可空类型的对象只在该对象为非空时才会装箱。如果 HasValue 为 false,则不装箱,而是将对象引用直接赋值为 null。例如:
bool? b = null;object o = b;// Now o is null.
如果对象非空,也就是说,如果 HasValue 为 true,则会发生装箱过程,但只将可空对象所基于的基础类型装箱。如果将非空的可空值类型装箱,将使值类型本身(而不是包装该值类型的 System.Nullable)装箱。例如:
bool? b = false;int? i = 44;object bBoxed = b; // bBoxed contains a boxed bool.object iBoxed = i; // iBoxed contains a boxed int.
对于那些通过装箱非可空类型而创建的类型来说,两种装箱对象是完全相同的。并且,像非可空装箱类型一样,可以将它们取消装箱,使其成为可空类型,如下所示:
bool? b2 = (bool?)bBoxed;int? i2 = (int?)iBoxed;