Nullable<T>.Explicit(Nullable<T> to T) 运算符
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义 Nullable<T> 实例到其基础值的显式转换。
public:
 static explicit operator T(Nullable<T> value);public static explicit operator T (T? value);static member op_Explicit : Nullable<'T (requires 'T : struct)> -> 'TPublic Shared Narrowing Operator CType (value As Nullable(Of T)) As T参数
- value
- Nullable<T>
可以为 Null 的值。
返回
- T
value 参数的 Value 属性的值。
示例
该Explicit运算符启用以下代码,它将值Int32转换为Nullable(Of Int32)值。
using System;
public class Example
{
   public static void Main()
   {
       var nullInt = new Nullable<int>(172);
       // Convert with CInt conversion method.
       Console.WriteLine((int)nullInt);
       // Convert with Convert.ChangeType.
       Console.WriteLine(Convert.ChangeType(nullInt, typeof(int)));
   }
}
// The example displays the following output:
//       172
//       172
open System
let nullInt = Nullable 172
// Convert with int conversion function.
printfn $"{int nullInt}"
// Convert with Convert.ChangeType.
printfn $"{Convert.ChangeType(nullInt, typeof<int>)}"
// The example displays the following output:
//       172
//       172
Module Example
   Public Sub Main()
       Dim nullInt = New Nullable(Of Integer)(172)
       ' Convert with CInt conversion method.
       Console.WriteLine(CInt(nullInt))
       ' Convert with CType conversion method.
       Console.WriteLine(CType(nullInt, Integer))
       ' Convert with Convert.ChangeType.
       Console.WriteLine(Convert.ChangeType(nullInt, GetType(Integer)))
   End Sub
End Module
' The example displays the following output:
'       172
'       172
'       172
注解
此运算符支持将当前Nullable<T>实例显式转换为类型T(类型)。Value 此类显式转换的语法依赖于语言。 还可以通过调用 Convert.ChangeType 方法来执行转换。
此运算符的等效方法为 Nullable<T>.Value