資料類型摘要 (Visual Basic)Data Type Summary (Visual Basic)

下表顯示 Visual Basic 的資料類型、其支援的通用語言執行平臺類型、其名義儲存體配置, 以及其值範圍。The following table shows the Visual Basic data types, their supporting common language runtime types, their nominal storage allocation, and their value ranges.

Visual Basic 類型Visual Basic type Common language runtime 類型結構Common language runtime type structure 名義儲存體配置Nominal storage allocation 數值範圍Value range
布林值Boolean Boolean 取決於執行平臺Depends on implementing platform TrueFalseTrue or False
ByteByte Byte 1 個位元組1 byte 0到 255 (不帶正負號)0 through 255 (unsigned)
Char(單一字元)Char (single character) Char 2 個位元組2 bytes 0到 65535 (不帶正負號)0 through 65535 (unsigned)
DateDate DateTime 8 個位元組8 bytes 0:00:00 (午夜), 位於0001年1月1日至 11:59:59 PM, 9999 年12月31日0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999
DecimalDecimal Decimal 16 個位元組16 bytes 0到 +/-79228162514264337593543950335 (+/-7.9...E + 28) , 沒有小數點;0到 +/-7.9228162514264337593543950335, 小數點右邊有28個位置;0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9...E+28) with no decimal point; 0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal;

最小的非零數位是 +/-0.0000000000000000000000000001 (+/-1E-28) smallest nonzero number is +/-0.0000000000000000000000000001 (+/-1E-28)
Double(雙精確度浮點數)Double (double-precision floating-point) Double 8 個位元組8 bytes -1.79769313486231570 e + 308 到-4.94065645841246544 E-324 代表負值;-1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values;

4.94065645841246544 e-324 到 1.79769313486231570 E + 308 的正值4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values
IntegerInteger Int32 4 個位元組4 bytes -2147483648 到 2147483647 (帶正負號)-2,147,483,648 through 2,147,483,647 (signed)
(長整數)Long (long integer) Int64 8 個位元組8 bytes -9223372036854775808 到 9223372036854775807 (9.2 ... E + 18 ) (帶正負號)-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 (9.2...E+18 ) (signed)
物件Object Object課堂Object (class) 32位平臺上4個位元組4 bytes on 32-bit platform

64位平臺上8個位元組8 bytes on 64-bit platform
任何類型都可以儲存在類型的變數中。ObjectAny type can be stored in a variable of type Object
SByteSByte SByte 1 個位元組1 byte -128 到 127 (帶正負號)-128 through 127 (signed)
Short(短整數)Short (short integer) Int16 2 個位元組2 bytes -32768 到 32767 (帶正負號)-32,768 through 32,767 (signed)
單一(單精確度浮點數)Single (single-precision floating-point) Single 4 個位元組4 bytes -3.4028235 e + 38 到-1.401298 E-45 代表負值;-3.4028235E+38 through -1.401298E-45 for negative values;

1.401298 e-45 到 3.4028235 E + 38 適用于正值1.401298E-45 through 3.4028235E+38 for positive values
字串(可變長度)String (variable-length) String課堂String (class) 取決於執行平臺Depends on implementing platform 0到大約2000000000的 Unicode 字元0 to approximately 2 billion Unicode characters
UIntegerUInteger UInt32 4 個位元組4 bytes 0到 4294967295 (不帶正負號)0 through 4,294,967,295 (unsigned)
ULongULong UInt64 8 個位元組8 bytes 0到 18446744073709551615 (1.8 ... E + 19 ) (不帶正負號)0 through 18,446,744,073,709,551,615 (1.8...E+19 ) (unsigned)
使用者定義表示User-Defined (structure) (繼承自ValueType)(inherits from ValueType) 取決於執行平臺Depends on implementing platform 結構的每個成員都有由其資料類型決定的範圍, 而且與其他成員的範圍無關Each member of the structure has a range determined by its data type and independent of the ranges of the other members
UShortUShort UInt16 2 個位元組2 bytes 0到 65535 (不帶正負號)0 through 65,535 (unsigned)

科學標記法中, "E" 是指10的乘冪。 In scientific notation, "E" refers to a power of 10. 因此, 3.56 E + 2 表示 3.56 x 102或 356, 而 3.56 e-2 表示 3.56/102或0.0356。So 3.56E+2 signifies 3.56 x 102 or 356, and 3.56E-2 signifies 3.56 / 102 or 0.0356.

注意

若為包含文字的字串, StrConv請使用函式, 從一種文字格式轉換成另一種。For strings containing text, use the StrConv function to convert from one text format to another.

除了在宣告語句中指定資料類型之外, 您還可以使用類型字元來強制某些程式設計項目的資料類型。In addition to specifying a data type in a declaration statement, you can force the data type of some programming elements by using a type character. 請參閱類型字元See Type Characters.

記憶體消耗量Memory Consumption

當您宣告基本資料類型時, 假設其記憶體耗用量與其名義儲存體配置相同, 就不安全。When you declare an elementary data type, it is not safe to assume that its memory consumption is the same as its nominal storage allocation. 這是因為下列考慮:This is due to the following considerations:

  • 儲存體指派。Storage Assignment. Common language runtime 可以根據您的應用程式執行所在平臺的目前特性來指派儲存體。The common language runtime can assign storage based on the current characteristics of the platform on which your application is executing. 如果記憶體幾乎已滿, 它可能會盡可能緊密地封裝您宣告的元素。If memory is nearly full, it might pack your declared elements as closely together as possible. 在其他情況下, 它可能會將其記憶體位址對齊自然硬體界限, 以優化效能。In other cases it might align their memory addresses to natural hardware boundaries to optimize performance.

  • 平臺寬度。Platform Width. 64位平臺上的儲存體指派與32位平臺上的指派不同。Storage assignment on a 64-bit platform is different from assignment on a 32-bit platform.

複合資料類型Composite Data Types

相同的考慮適用于複合資料型別的每個成員, 例如結構或陣列。The same considerations apply to each member of a composite data type, such as a structure or an array. 您不能只是將類型成員的名義儲存配置加在一起。You cannot rely on simply adding together the nominal storage allocations of the type's members. 此外, 還有其他考慮, 如下所示:Furthermore, there are other considerations, such as the following:

  • 投影機.Overhead. 有些複合類型有額外的記憶體需求。Some composite types have additional memory requirements. 例如, 陣列本身會使用額外的記憶體, 也會針對每個維度使用。For example, an array uses extra memory for the array itself and also for each dimension. 在32位平臺上, 此額外負荷目前為12個位元組, 加上每個維度8個位元組。On a 32-bit platform, this overhead is currently 12 bytes plus 8 bytes for each dimension. 在64位平臺上, 這項需求加倍。On a 64-bit platform this requirement is doubled.

  • 儲存版面配置。Storage Layout. 您無法安全地假設記憶體中的儲存順序與您的宣告順序相同。You cannot safely assume that the order of storage in memory is the same as your order of declaration. 您甚至無法對位元組對齊進行假設, 例如2位元組或4位元組的界限。You cannot even make assumptions about byte alignment, such as a 2-byte or 4-byte boundary. 如果您要定義類別或結構, 而且需要控制其成員的儲存配置, 您可以將StructLayoutAttribute屬性 (attribute) 套用至類別或結構。If you are defining a class or structure and you need to control the storage layout of its members, you can apply the StructLayoutAttribute attribute to the class or structure.

物件負荷Object Overhead

除了資料類型中包含的資料以外,參考任何基本或複合資料型別的也會使用4個位元組。ObjectAn Object referring to any elementary or composite data type uses 4 bytes in addition to the data contained in the data type.

另請參閱See also