系列目录    【已更新最新开发文章,点击查看详细】 <https://www.cnblogs.com/SavionZhang/p/11229640.html>
  可以为 null 的类型是 System.Nullable<T>
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1> 结构的实例。 可以为 null
的类型可表示一个基础类型的所有值T,还可以再表示一个 null
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/null>
值。基础类型 T 可以是任何不可为 null 的值类型
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/value-types>
。T 不能是引用类型。

例如,可以将 null 或任何整数值(从 Int32.MinValue
<https://docs.microsoft.com/zh-cn/dotnet/api/system.int32.minvalue> 到
Int32.MaxValue
<https://docs.microsoft.com/zh-cn/dotnet/api/system.int32.maxvalue>)赋给
Nullable<int>,并可将 true
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/true-literal>
false
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/false-literal>
或null 赋给Nullable<bool>。

需要表示基础类型的未定义的值时,请使用可以为 null 的类型。 布尔变量只能有两个值:true 和 false。 没有“未定义”的值。
在许多编程应用程序中,尤其是数据库交互中,变量值可能未定义或缺失。例如,数据库中的字段可能包含值 true 或 false,但它也可能根本不包含任何值。
这种情况下要使用Nullable<bool> 类型。

可以为 null 的类型具有以下特征:

*
可以为 null 的类型表示可以向其赋与 null 值的值类型变量。 不能根据引用类型创建可以为 null 的类型 (引用类型已支持 null 值)。

*
语法 T? 是 Nullable<T> 的简写。 这两种形式是可互换的。

*
向可以为 null 的类型赋值的方法与向基础值类型赋值的方法相同:int? x = 10; 或 double? d = 4.108;。 还可赋予 null
值:int? x = null;。

*
使用 Nullable<T>.HasValue
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.hasvalue> 和
Nullable<T>.Value
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.value>
只读属性可测试是否存在 null 值并检索值,如以下示例所示:if (x.HasValue) y = x.Value;

*
如果变量包含值,则 HasValue
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.hasvalue> 属性返回
true;如果值为 null,则返回 false。

*
如果 HasValue
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.hasvalue> 返回 true
,则Value <https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.value>
属性返回值。否则会引发 InvalidOperationException
<https://docs.microsoft.com/zh-cn/dotnet/api/system.invalidoperationexception>。

*
还可将 == 和 != 运算符用于可以为 null 的类型,如以下示例所示:if (x != null) y = x.Value; 如果 a 和 b 均为
null,则a == b 的计算结果为 true。

*
从 C# 7.0 开始,可以使用模式匹配
<https://docs.microsoft.com/zh-cn/dotnet/csharp/pattern-matching#the-is-type-pattern-expression>
来检查和获取可以为 null 的类型的值:if (x is int valueOfX) y = valueOfX;。

*
T? 的默认值是一个实例,其 HasValue
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.hasvalue> 属性返回
false。

*
使用 GetValueOrDefault()
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.getvalueordefault#System_Nullable_1_GetValueOrDefault>
方法可返回赋予的值,如果可以为 null 的类型的值为null,它还可返回基础值类型的默认
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/default-values-table>
值。

*
使用 GetValueOrDefault(T)
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1.getvalueordefault#System_Nullable_1_GetValueOrDefault__0_>
方法可返回赋予的值,如果可以为 null 的类型的值为null,它还可返回提供的默认值。

*
使用 null 合并运算符
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/null-coalescing-operator>
??,基于可以为 null 的类型的值向基础类型赋值:int? x = null; int y = x ?? -1;。 在示例中,由于 x 为 null,所以
y 的结果值为 -1。

*
如果定义了(用户定义的)两种数据类型之间的转换,还可将同一转换用于这些数据类型的可为 null 的版本。

*
不得嵌套可以为 null 的类型。 不会编译下面的一行代码:Nullable<Nullable<int>> n;

有关详细信息,请参阅使用可以为 null 的类型
<https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/nullable-types/using-nullable-types>
,以及如何:标识可以为 null 的类型
<https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/nullable-types/how-to-identify-a-nullable-type>
主题。

请参阅

* System.Nullable<T>
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable-1>
* System.Nullable
<https://docs.microsoft.com/zh-cn/dotnet/api/system.nullable>
* ??运算符
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/null-coalescing-operator>
* C# 编程指南
<https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/index>
* C# 指南 <https://docs.microsoft.com/zh-cn/dotnet/csharp/index>
* C# 参考
<https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/index>
* 可以为 Null 的值类型 (Visual Basic)
<https://docs.microsoft.com/zh-cn/dotnet/visual-basic/programming-guide/language-features/data-types/nullable-value-types>
 
系列目录    【已更新最新开发文章,点击查看详细】
<https://www.cnblogs.com/SavionZhang/p/11229640.html>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信