實值型別 (C# 參考)Value types (C# Reference)

有兩種實值型別:There are two kinds of value types:

實值型別的主要功能Main features of value types

實值型別的變數會包含該型別的值。A variable of a value type contains a value of the type. 例如,int 型別的變數可能包含 42值。For example, a variable of the int type might contain the value 42. 這與參考型別的變數不同,參考型別的變數會包含型別執行個體 (也稱為物件) 的參考。This differs from a variable of a reference type, which contains a reference to an instance of the type, also known as an object. 當您指派新值給某個實值型別的變數時,系統會複製該值。When you assign a new value to a variable of a value type, that value is copied. 當您指派新值給某個參考型別的變數時,系統會複製參考,而不是物件本身。When you assign a new value to a variable of a reference type, the reference is copied, not the object itself.

所有實值型別都是隱含地衍生自 System.ValueTypeAll value types are derived implicitly from the System.ValueType.

與參考型別不同,您無法從實值型別衍生新的類型。Unlike with reference types, you cannot derive a new type from a value type. 不過,就像參考型別,結構可以實作介面。However, like reference types, structs can implement interfaces.

實值型別預設不可為 nullValue type variables cannot be null by default. 不過,相對應的可為 Null 的型別則可為 nullHowever, variables of the corresponding nullable types can be null.

每種實值型別都具有初始化該型別預設值的隱含無參數建構函式。Each value type has an implicit parameterless constructor that initializes the default value of that type. 如需有關實值型別預設值的資訊,請參閱預設值表For information about default values of value types, see Default values table.

簡單型別Simple types

「簡單型別」 是 C# 所提供的一組預先定義結構型別,其中包含下列型別:The simple types are a set of predefined struct types provided by C# and comprise the following types:

透過關鍵字即可識別簡單型別,但這些關鍵字只是 System 命名空間中預先定義之結構型別的別名。The simple types are identified through keywords, but these keywords are simply aliases for predefined struct types in the System namespace. 例如,intSystem.Int32 的別名。For example, int is an alias of System.Int32. 如需完整的別名清單,請參閱內建型別表For a complete list of aliases, see Built-in types table.

簡單型別與其他結構型別的差異在於它們可允許某些額外的作業:The simple types differ from other struct types in that they permit certain additional operations:

  • 簡單類型可以使用常值進行初始化。Simple types can be initialized by using literals. 例如,'A'char 型別的常值,而 2001 則是 int 型別的常值。For example, 'A' is a literal of the type char and 2001 is a literal of the type int.

  • 您可以使用 const 關鍵字來宣告簡單型別的常數。You can declare constants of the simple types with the const keyword. 其他結構型別則不可能有常數。It's not possible to have constants of other struct types.

  • 常數運算式如果運算元全都是簡單型別常數,就會在編譯階段進行評估。Constant expressions, whose operands are all simple type constants, are evaluated at compile time.

如需詳細資訊,請參閱 C# 語言規格簡單型別一節。For more information, see the Simple types section of the C# language specification.

將實值型別初始化Initializing value types

必須先初始化 C# 中的區域變數,再使用它們。Local variables in C# must be initialized before they are used. 例如,您可以宣告區域變數,而不進行初始化,如下列範例所示:For example, you might declare a local variable without initialization as in the following example:

int myInt;

您不能先使用它,再將它初始化。You cannot use it before you initialize it. 您可以使用下列陳述式來初始化執行個體:You can initialize it using the following statement:

myInt = new int();  // Invoke parameterless constructor for int type.

這個陳述式相當於下列陳述式:This statement is equivalent to the following statement:

myInt = 0;         // Assign an initial value, 0 in this example.

當然,您可以有相同陳述式中的宣告和初始化,如下列範例所示:You can, of course, have the declaration and the initialization in the same statement as in the following examples:

int myInt = new int();

-或-–or–

int myInt = 0;

使用 new 運算子會呼叫特定型別的無參數建構函式,並將預設值指派給變數。Using the new operator calls the parameterless constructor of the specific type and assigns the default value to the variable. 在上述範例中,無參數建構函式已將 0 值指派給 myIntIn the preceding example, the parameterless constructor assigned the value 0 to myInt. 如需有關透過呼叫預設建構函式來指派的值之詳細資訊,請參閱預設值表For more information about values assigned by calling default constructors, see Default values table.

如果要使用使用者定義型別,請使用 new 來叫用無參數建構函式。With user-defined types, use new to invoke the parameterless constructor. 例如,下列陳述式會叫用 Point 結構的無參數建構函式:For example, the following statement invokes the parameterless constructor of the Point struct:

Point p = new Point(); // Invoke parameterless constructor for the struct.

在此呼叫之後,會視為已明確指派結構,也就是說,其所有成員都已初始化為其預設值。After this call, the struct is considered to be definitely assigned; that is, all its members are initialized to their default values.

如需有關 new 運算子的詳細資訊,請參閱 newFor more information about the new operator, see new.

如需有關將數字型別輸出格式化的資訊,請參閱格式化數值結果表For information about formatting the output of numeric types, see Formatting numeric results table.

另請參閱See also