天天看点

(转载)C# 编程 使用可空类型

转载出处为://https://msdn.microsoft.com/zh-cn/library/2cf62fcy(vs.90).aspx#Anchor_1

C#可以为 null 的类型可以表示基础类型的所有值,另外还可以表示 null 值。

实际用法一:数据库中的值类型字段是可以为null,比如数据类型为int 的字段,你如何在代码中体现数据库中这个字段值类型是 int ,同时它的实际值又是 null 。

可以为 null 的类型可通过下面两种方式中的一种声明:

System.Nullable<T> variable - 或 - T? variable

T 是可以为 null 的类型的基础类型。T 可以是包括 struct 在内的任何值类型;但不能是引用类型。

有关可能使用可以为 null 的类型的示例,请考虑普通的布尔变量如何能够具有两个值:true 和 false。不存在表示“未定义”的值。在很多编程应用中(最突出的是数据库交互),变量可以以未定义的状态出现。例如,数据库中的某个字段可能包含值 true 或 false,但是它也可能根本不包含值。同样,可以将引用类型设置为 null,以指示它们未初始化。

这种不一致会导致额外的编程工作,如使用附加变量来存储状态信息、使用特殊值,等等。可以为 null 的类型修饰符使 C# 能够创建表示未定义值的值类型变量。

可以为 null 的类型示例

 任何值类型都可用作可以为 null 的类型的基础。例如:

int? i = 10;
double? d1 = 3.14;
bool? flag = null;
char? letter = 'a';
int?[] arr = new int?[10];      

可以为 null 的类型的成员

 1.可以为 null 的类型的每个实例都具有两个公共的只读属性:

  • HasValue

    HasValue 属于 bool 类型。当变量包含非 null 值时,它被设置为 true。

  • Value

    Value 的类型与基础类型相同。如果 HasValue 为 true,则说明 Value 包含有意义的值。如果 HasValue 为 false,则访问Value 将引发 InvalidOperationException。

在此示例中,HasValue 成员用于在尝试显示变量之前测试它是否包含值。

int? x = 10;
if (x.HasValue)
{
    //console.write 10
    System.Console.WriteLine(x.Value);
}
else
{
    System.Console.WriteLine("Undefined");
}      

2.也可以如下面的示例所示对值进行测试:

int? y = 10;
if (y != null)
{
   //console.write 10
    System.Console.WriteLine(y.Value);
}
else
{
    System.Console.WriteLine("Undefined");
}      

显式转换

可以为 null 的类型可强制转换为常规类型,方法是使用强制转换来显式转换或者通过使用 Value 属性来转换。例如:

int? n = null;

//int m1 = n;      // Will not compile.
int m2 = (int)n;   // Compiles, but will create an exception if x is null.
int m3 = n.Value;  // Compiles, but will create an exception if x is null.
//编译器报错,因为不能可为空的m2 or m3 对象必须具有一个值。      

如果两种数据类型之间定义了用户定义的转换,则同一转换也可用于这些数据类型的可以为 null 的版本。

隐式转换

可使用 null 关键字将可以为 null 的类型的变量设置为 null,如以下示例所示:

int? n1 = null;      

从普通类型到可以为 null 的类型的转换是隐式的。

int? n2;
n2 = 10;  // Implicit conversion.      

运算符

可以为 null 的类型还可以使用预定义的一元和二元运算符,以及现有的任何用户定义的值类型运算符。如果操作数为 null,这些运算符将产生一个 null 值;否则运算符将使用包含的值来计算结果。例如:

int? a = 10;
int? b = null;

a++;         // Increment by 1, now a is 11.
a = a * 10;  // Multiply by 10, now a is 110.
a = a + b;   // Add b, now a is null. //a变成了null      

在执行可以为 null 的类型的比较时,如果其中一个可以为 null 的类型为 null,则比较结果将始终为 false。因此,一定不要以为由于一个比较结果为 false,相反的情况就会为 true。例如:

int? num1 = 10;
int? num2 = null;
if (num1 >= num2)
{
    System.Console.WriteLine("num1 is greater than or equal to num1");
}
else
{
     //输出else,无法比较,为false
    // num1 is NOT less than num2
}      

else 语句中的结论无效,因为 num2 为 null,因此不包含值。

比较两个均为 null 的可以为 null 的类型时结果为 true。

??运算符

?? 运算符定义在将可以为 null 的类型分配给非可以为 null 的类型时返回的默认值。

int? c = null;

// d = c, unless c is null, in which case d = -1.
int d = c ?? -1; // d = -1
// if c has value,so output e value      

此运算符还可用于多个可以为 null 的类型。例如:

int? e = null;
int? f = null;

// g = e or f, unless e and f are both null, in which case g = -1.
int g = e ?? f ?? -1; //g =  -1
// if e has value,so output e value,      

bool? 类型

bool? 可以为 null 的类型可以包含三个不同的值:true、false 和 null。因此,bool? 类型不能用于条件语句,如 if、for 或 while。例如,此代码无法编译,并将报告编译器错误 CS0266:

bool? b = null;
if (b) // Error CS0266.
{
}      

这是不允许的,因为 null 在条件上下文中的含义并不清楚。若要在条件语句中使用 bool?,请首先检查其 HasValue 属性以确保其值不是 null,然后将它强制转换为 bool。有关更多信息,请参见 bool。如果对使用 null 值的 bool? 执行强制转换,则在条件测试中将引发InvalidOperationException。下面的示例演示了一种从 bool? 安全地强制转换为 bool 的方法:

bool? test = null;
             // Other code that may or may not
                // give a value to test.
            if(!test.HasValue) //check for a value
            {
                // Assume that IsInitialized
                // IsInitialized() returns either true or false.
                test = IsInitialized(); 
            }
            if((bool)test) //now this cast is safe
            {
               // Do something.
            }      

可以为 null 的布尔值类似于 SQL 中使用的布尔变量类型。为了确保 & 和 | 运算符产生的结果与 SQL 中的三值布尔类型一致,提供了以下预定义的运算符:

bool? operator &(bool? x, bool? y)

bool? operator |(bool? x, bool? y)

下表中列出了这些运算符的结果:

X y x&y x|y
true
false
null