PackingSize 列舉


指定兩個因數其中之一,在將類型封送處理時,該因數決定欄位的記憶體對齊。Specifies one of two factors that determine the memory alignment of fields when a type is marshaled.

此列舉有 FlagsAttribute 屬性,因此其成員值可進行位元組合。

public enum class PackingSize
public enum PackingSize
public enum PackingSize
public enum PackingSize
type PackingSize = 
type PackingSize = 
type PackingSize = 
Public Enum PackingSize


Size1 1

封裝大小為 1 位元組。The packing size is 1 byte.

Size128 128

封裝大小為 128 位元組。The packing size is 128 bytes.

Size16 16

封裝大小為 16 位元組。The packing size is 16 bytes.

Size2 2

封裝大小為 2 位元組。The packing size is 2 bytes.

Size32 32

封裝大小為 32 位元組。The packing size is 32 bytes.

Size4 4

封裝大小為 4 位元組。The packing size is 4 bytes.

Size64 64

封裝大小為 64 位元組。The packing size is 64 bytes.

Size8 8

封裝大小為 8 位元組。The packing size is 8 bytes.

Unspecified 0

封裝大小未指定。The packing size is not specified.


封裝大小會影響組織的結構和類別中欄位的對齊方式 LayoutKind.SequentialPacking size affects the alignment of fields in structures and classes whose organization is LayoutKind.Sequential. 封裝大小是在封送處理結構或類別時,判斷欄位位移的兩個因素之一:另一個因素是欄位的有效大小。The packing size is one of two factors that determine the offset of a field when the structure or class is marshaled; the other factor is the effective size of the field. 若要判斷欄位的位移:To determine the offset of a field:

  1. 將上述欄位的大小新增至上述欄位的位移,以計算位移的最小值。Compute the minimum value for the offset by adding the size of the preceding field to the offset of the preceding field.

  2. 計算欄位的自然封裝大小:如果欄位是簡單的資料類型,請使用大小。Compute the natural packing size for the field: If the field is a simple data type, use the size. 如果欄位是陣列,請使用陣列元素的大小。If the field is an array, use the size of an array element. 如果欄位本身是結構,請將該結構的封裝大小與該結構中最大欄位的大小進行比較,並使用這兩個值中較小的一個。If the field is itself a structure, compare the packing size of that structure to the size of the largest field in that structure, and use the smaller of the two values.

  3. 藉由比較封裝大小與欄位的自然封裝大小(在步驟2中計算)來計算有效的封裝大小。Compute the effective packing size by comparing the packing size to the natural packing size of the field, computed in step 2. 使用兩個數字的較小者。Use the smaller of the two numbers.

  4. 計算位移。Compute the offset. 位移是等於或大於最小值的第一個記憶體位置,以及在步驟3中計算出的有效封裝大小的倍數。The offset is the first memory location that is equal to or larger than the minimum, and a multiple of the effective packing size computed in step 3.


一旦為類別或結構中的所有欄位計算位移之後,就會加入填補,使整體大小成為封裝大小的倍數。Once offsets have been computed for all the fields in the class or structure, padding is added so that the overall size is a multiple of the packing size.

例如,在包含和的類別中 byte long ,使用封裝大小 Size1 ,欄位的位移 byte 是零 (0) 而欄位的位移 long 是一個 (1) 。For example, in a class containing a byte and a long, using packing size Size1, the offset of the byte field is zero (0) and the offset of the long field is one (1). 如果封裝大小為 Size4 ,欄位的 long 位移四 (4) 。If the packing size is Size4, the long field has offset four (4). 在包含兩個欄位的類別中 short ,使用任何封裝大小,第二個 short 欄位的位移是兩個 (2) 。In a class containing two short fields, using any packing size, the offset of the second short field is two (2).


變更封裝大小可能會影響效能,因為它會使欄位對齊其大小不是倍數的位移。Changing the packing size can affect performance if it causes fields to be aligned on offsets that are not multiples of their size.