值类型(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 类型的变量可以包含值 42For 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. 但相应的可为空的值类型的变量可以为 nullHowever, variables of the corresponding nullable value 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 parameterless 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:

var 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