運算子結果的資料類型 (Visual Basic)

Visual Basic 會根據運算元的資料類型判斷運算的結果資料類型。 在某些情況下,該資料類型的範圍可能會比任一運算元的範圍都要來的大。

資料類型範圍

下方列出了相關資料類型的範圍,順序從最小到最大:

  • Boolean — 有兩個可能的值

  • SByteByte — 有 256 個可能的整數值

  • ShortUShort — 有 65,536 (6.5...E+4) 個可能的整數值

  • IntegerUInteger — 有 4,294,967,296 (4.2...E+9) 個可能的整數值

  • LongULong — 有 18,446,744,073,709,551,615 (1.8...E+19) 個可能的整數值

  • Decimal — 有 1.5...E+29 個可能的整數值,最大範圍為 7.9...E+28 (絕對值)

  • Single — 最大範圍為 3.4...E+38 (絕對值)

  • Double — 最大範圍為 1.7...E+308 (絕對值)

如需 Visual Basic 資料類型的詳細資訊,請參閱資料類型

若運算元評估為 Nothing,則 Visual Basic 算術運算子會將其視為零。

十進位算術

請注意,Decimal 資料類型不是浮點數,也不是整數。

+*/Mod 運算的其中一個運算元為 Decimal,且另一個運算元不是 SingleDouble,則 Visual Basic 會將另一個運算元擴展為 Decimal。 Visual Basic 會以 Decimal 執行運算,因此結果資料類型會是 Decimal

浮點數算術

Visual Basic 會以 Double 執行大部分的浮點數算術,對這類運算而言,這是最有效率的資料類型。 不過,若一個運算元為 Single,而另一個不是 Double,則 Visual Basic 會以 Single 執行運算。 Visual Basic 會在運算前視需要將每個運算元擴展為適當的資料類型,且結果會使用該資料類型。

/ 和 ^ 運算子

/ 運算子只會針對 DecimalSingleDouble 資料類型進行定義。 Visual Basic 會在運算前視需要將每個運算元擴展為適當的資料類型,且結果會使用該資料類型。

下表說明了 / 運算子的結果資料類型。 請注意,此資料表是對稱的;針對運算元資料類型的特定組合,無論運算元的順序為何,結果資料類型都會相同。

Decimal Single Double 任何整數類型
Decimal Decimal Single Double Decimal
Single Single Single Double Single
Double Double Double Double Double
任何整數類型 Decimal Single Double Double

^ 運算子只會針對 Double 資料類型進行定義。 Visual Basic 會在運算前視需要將每個運算元擴展為 Double,且結果資料類型會一律為 Double

整數算術

整數運算的結果資料類型取決於運算元的資料類型。 一般而言,Visual Basic 會使用下列原則來判斷結果資料類型:

  • 若二元運算子的兩個運算元具有相同的資料類型,則結果也會使用該資料類型。 例外狀況為 Boolean,此狀況會強制轉換為 Short

  • 若一起使用不帶正負號的運算元和帶正負號的運算元,則結果也會是帶正負號的類型,且範圍至少會與任一運算元一樣大。

  • 否則,結果的資料類型通常會是兩個運算元資料類型中的較大者。

請注意,結果資料類型可能與任一運算元資料類型都不同。

注意

結果資料類型不一定大到足以包含運算所產生的所有可能值。 若值對結果資料類型來說過大,則可能會發生 OverflowException 例外狀況。

一元 + 和 – 運算子

下表說明了 + 這兩個一元運算子的結果資料類型。

Boolean SByte Byte Short UShort Integer UInteger Long ULong
一元 + Short SByte Byte Short UShort 整數 UInteger Long ULong
一元 Short SByte Short Short 整數 整數 Long Long Decimal

<< 和 >> 運算子

下表說明了 <<>> 這兩個位元移位運算子的結果資料類型。 Visual Basic 會將每個位元移位運算子視作位於其左側運算元的一元運算子 (要位移的位元模式)。

Boolean SByte Byte Short UShort Integer UInteger Long ULong
<<, >> Short SByte Byte Short UShort 整數 UInteger Long ULong

若左側運算元為 DecimalSingleDoubleString,則 Visual Basic 會在運算前嘗試將其轉換為 Long,且結果資料類型會是 Long。 右側運算元 (要位移的位元位置數目) 必須是 Integer 或擴展為 Integer 的類型。

二元 +、–、* 和 Mod 運算子

下表說明了二元 + 運算子,以及 *Mod 運算子的結果資料類型。 請注意,此資料表是對稱的;針對運算元資料類型的特定組合,無論運算元的順序為何,結果資料類型都會相同。

Boolean SByte Byte Short UShort Integer UInteger Long ULong
Boolean Short SByte Short Short 整數 整數 Long Long Decimal
SByte SByte SByte Short Short 整數 整數 Long Long Decimal
Byte Short Short Byte Short UShort 整數 UInteger Long ULong
Short Short Short Short Short 整數 整數 Long Long Decimal
UShort 整數 整數 UShort 整數 UShort 整數 UInteger Long ULong
Integer 整數 整數 整數 整數 整數 整數 Long Long Decimal
UInteger Long Long UInteger Long UInteger Long UInteger Long ULong
Long Long Long Long Long Long Long Long Long Decimal
ULong Decimal Decimal ULong Decimal ULong Decimal ULong Decimal ULong

\ 運算子

下表說明了 \ 運算子的結果資料類型。 請注意,此資料表是對稱的;針對運算元資料類型的特定組合,無論運算元的順序為何,結果資料類型都會相同。

Boolean SByte Byte Short UShort Integer UInteger Long ULong
Boolean Short SByte Short Short 整數 整數 Long Long Long
SByte SByte SByte Short Short 整數 整數 Long Long Long
Byte Short Short Byte Short UShort 整數 UInteger Long ULong
Short Short Short Short Short 整數 整數 Long Long long
UShort 整數 整數 UShort 整數 UShort 整數 UInteger Long ULong
Integer 整數 整數 整數 整數 整數 整數 Long Long Long
UInteger Long Long UInteger Long UInteger Long UInteger Long ULong
Long Long Long Long Long Long Long Long Long Long
ULong Long Long ULong Long ULong Long ULong Long ULong

\ 運算子的任一運算元為 DecimalSingleDouble,則 Visual Basic 會在運算前嘗試將其轉換為 Long,且結果資料類型會是 Long

關係運算和位元運算的比較

關係運算 (=<><><=>=) 的結果資料類型一律會是 BooleanBoolean 資料類型。 對於 Boolean 運算元的邏輯運算 (AndAndAlsoNotOrOrElseXor) 而言亦是如此。

位元邏輯運算的結果資料類型取決於運算元的資料類型。 請注意,AndAlsoOrElse 只會針對 Boolean 進行定義,且 Visual Basic 會在執行運算前視需要將每個運算元轉換為 Boolean

=、<>、<、>、<= 和 >= 運算子

若兩個運算元都是 Boolean,則 Visual Basic 會將 True 視作小於 False。 若將數值類型與 String 進行比較,則 Visual Basic 會在運算前嘗試將 String 轉換為 DoubleCharDate 運算元只能與資料類型相同的另一個運算元進行比較。 結果資料類型一律會是 Boolean

位元 Not 運算子

下表說明了位元 Not 運算子的結果資料類型。

Boolean SByte Byte Short UShort Integer UInteger Long ULong
Not 布林值 SByte Byte Short UShort 整數 UInteger Long ULong

若運算元為 DecimalSingleDoubleString,則 Visual Basic 會在運算前嘗試將其轉換為 Long,且結果資料類型會是 Long

位元 And、Or 和 Xor 運算

下表說明了位元 AndOrXor 運算子的結果資料類型。 請注意,此資料表是對稱的;針對運算元資料類型的特定組合,無論運算元的順序為何,結果資料類型都會相同。

Boolean SByte Byte Short UShort Integer UInteger Long ULong
Boolean 布林值 SByte Short Short 整數 整數 Long Long Long
SByte SByte SByte Short Short 整數 整數 Long Long Long
Byte Short Short Byte Short UShort 整數 UInteger Long ULong
Short Short Short Short Short 整數 整數 Long Long long
UShort 整數 整數 UShort 整數 UShort 整數 UInteger Long ULong
Integer 整數 整數 整數 整數 整數 整數 Long Long Long
UInteger Long Long UInteger Long UInteger Long UInteger Long ULong
Long Long Long Long Long Long Long Long Long Long
ULong Long Long ULong Long ULong Long ULong Long ULong

若運算元為 DecimalSingleDoubleString,則 Visual Basic 會在運算前嘗試將其轉換為 Long,且結果資料類型會相同,就像運算元已經是 Long 一樣。

雜項運算子

& 運算子只會針對 String 運算元的串連進行定義。 Visual Basic 會在運算前視需要將每個運算元轉換為 String,且結果資料類型會一律為 String。 為了達到 & 運算子的目的,所有 String 的轉換都會視作擴展,即使 Option StrictOn 也是如此。

IsIsNot 運算子會要求兩個運算元都屬於參考型別。 TypeOf...Is 運算式要求第一個運算元是參考型別,且第二個運算元是資料類型名稱。 在上述所有情況下,結果資料類型都是 Boolean

Like 運算子只會針對 String 運算元的模式比對進行定義。 Visual Basic 會在運算前視需要嘗試將每個運算元轉換為 String。 結果資料類型一律會是 Boolean

另請參閱