// C#
double d = 1.234;
int i = (int)Convert.ChangeType(d, typeof(int));
// VB
Dim d As Double = 1.234
Dim i As Integer = CInt(Convert.ChangeType(d, GetType(Integer)))
(型別)運算式CType(運算式, 型別)(內聯函數,效能佳)。CBool, CInt, CStr, Hex, Format, Val 等皆為內聯函數。
| 關鍵字 | 適用型別 | 型別關係要求 |
|---|---|---|
| CType | 任意 | 須有擴大/縮小轉換定義 |
| DirectCast | 任意 | 一型別須繼承/實作另一型別 |
| TryCast | 僅參考類型 | 一型別須繼承/實作另一型別 |
| as (C#) | 僅參考類型 | 相容參考型別 |
TryCast 對應 C# as,轉換失敗回傳 Nothing/null 不拋例外。