Nullable<T>.Explicit(Nullable<T> to T) 運算子

定義

定義 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)> -> 'T
Public Shared Narrowing Operator CType (value As Nullable(Of T)) As T

參數

value
Nullable<T>

可為 Null 的值。

傳回

T

value 參數之 Value 屬性的值。

範例

運算子 Explicit 會啟用下列程式碼,將值轉換成 Nullable(Of Int32) 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

適用於