类型Types

C# 语言的类型分为两个主要类别:值类型引用类型The types of the C# language are divided into two main categories: value types and reference types. 值类型和引用类型可能泛型类型,这需要一个或多个类型参数Both value types and reference types may be generic types, which take one or more type parameters. 类型参数可以指定这两种值类型和引用类型。Type parameters can designate both value types and reference types.

type
    : value_type
    | reference_type
    | type_parameter
    | type_unsafe
    ;

仅在不安全代码中提供的类型、 指针、 的最后一个类别。The final category of types, pointers, is available only in unsafe code. 对此进行讨论中进一步指针类型This is discussed further in Pointer types.

值类型不同于引用类型,值类型的变量直接包含其数据,而变量引用的类型应用商店引用对其数据,后者称为对象.Value types differ from reference types in that variables of the value types directly contain their data, whereas variables of the reference types store references to their data, the latter being known as objects. 对于引用类型,它是两个变量来引用同一对象,并因此可能会对一个变量以影响其他变量所引用的对象的操作。With reference types, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. 对于值类型,每个变量都有自己的数据的副本并不是可能会影响另一个操作。With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.

C# 类型系统被统一的这样可以任何类型的值视为对象。C#'s type system is unified such that a value of any type can be treated as an object. 每种 C# 类型都直接或间接地派生自 object 类类型,而 object 是所有类型的最终基类。Every type in C# directly or indirectly derives from the object class type, and object is the ultimate base class of all types. 只需将值视为类型 object,即可将引用类型的值视为对象。Values of reference types are treated as objects simply by viewing the values as type object. 值类型的值作为对象处理通过执行装箱和取消装箱操作 (装箱和取消装箱)。Values of value types are treated as objects by performing boxing and unboxing operations (Boxing and unboxing).

值类型Value types

值类型是结构类型或枚举类型。A value type is either a struct type or an enumeration type. C# 提供了一组预定义的结构类型称为简单类型C# provides a set of predefined struct types called the simple types. 通过保留字,简单类型进行标识。The simple types are identified through reserved words.

value_type
    : struct_type
    | enum_type
    ;

struct_type
    : type_name
    | simple_type
    | nullable_type
    ;

simple_type
    : numeric_type
    | 'bool'
    ;

numeric_type
    : integral_type
    | floating_point_type
    | 'decimal'
    ;

integral_type
    : 'sbyte'
    | 'byte'
    | 'short'
    | 'ushort'
    | 'int'
    | 'uint'
    | 'long'
    | 'ulong'
    | 'char'
    ;

floating_point_type
    : 'float'
    | 'double'
    ;

nullable_type
    : non_nullable_value_type '?'
    ;

non_nullable_value_type
    : type
    ;

enum_type
    : type_name
    ;

与引用类型的变量,不同的变量的值类型可以包含值null仅当值类型为 null 的类型。Unlike a variable of a reference type, a variable of a value type can contain the value null only if the value type is a nullable type. 对于每个不可为 null 的值类型没有相应的可以为 null 值类型表示一组相同的值以及值nullFor every non-nullable value type there is a corresponding nullable value type denoting the same set of values plus the value null.

值类型的变量进行赋值会创建一份所赋的值。Assignment to a variable of a value type creates a copy of the value being assigned. 这不同于分配给变量的引用类型,复制引用而不是由引用标识的对象。This differs from assignment to a variable of a reference type, which copies the reference but not the object identified by the reference.

System.ValueType 类型The System.ValueType type

所有值类型隐式都继承自类System.ValueType,而后者又继承自类objectAll value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object. 不能为任何类型派生自值类型,并因此隐式密封的值类型 (密封类)。It is not possible for any type to derive from a value type, and value types are thus implicitly sealed (Sealed classes).

请注意,System.ValueType本身不是value_typeNote that System.ValueType is not itself a value_type. 相反,它是class_type所有value_type会自动派生 s。Rather, it is a class_type from which all value_types are automatically derived.

默认构造函数Default constructors

所有值类型隐式都声明公共无参数实例构造函数调用默认构造函数All value types implicitly declare a public parameterless instance constructor called the default constructor. 默认构造函数将返回零初始化的实例称为默认值对于值类型:The default constructor returns a zero-initialized instance known as the default value for the value type:

  • 为所有simple_types,默认值是生成全为零的位模式的值:For all simple_types, the default value is the value produced by a bit pattern of all zeros:
    • 有关sbytebyteshortushortintuintlong,并且ulong,默认值是0For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.
    • 有关char,默认值是'\x0000'For char, the default value is '\x0000'.
    • 有关float,默认值是0.0fFor float, the default value is 0.0f.
    • 有关double,默认值是0.0dFor double, the default value is 0.0d.
    • 有关decimal,默认值是0.0mFor decimal, the default value is 0.0m.
    • 有关bool,默认值是falseFor bool, the default value is false.
  • 有关enum_type E,默认值为0转换为的类型、 EFor an enum_type E, the default value is 0, converted to the type E.
  • 有关struct_type,默认值是生成通过将所有值类型字段都设置为其默认值和所有引用类型字段的值nullFor a struct_type, the default value is the value produced by setting all value type fields to their default value and all reference type fields to null.
  • 有关nullable_type的默认值是为其实例HasValue属性为 false 和Value未定义属性。For a nullable_type the default value is an instance for which the HasValue property is false and the Value property is undefined. 默认值是也称为null 值的为 null 的类型。The default value is also known as the null value of the nullable type.

值类型的默认构造函数调用使用任何其他实例构造函数,如new运算符。Like any other instance constructor, the default constructor of a value type is invoked using the new operator. 出于效率考虑,此要求不适用于实际具有生成的构造函数调用的实现。For efficiency reasons, this requirement is not intended to actually have the implementation generate a constructor call. 在以下示例中,变量ij均被初始化为零。In the example below, variables i and j are both initialized to zero.

class A
{
    void F() {
        int i = 0;
        int j = new int();
    }
}

每个值类型隐式具有公共无参数实例构造函数,因为它不能包含无参数构造函数的显式声明为结构类型。Because every value type implicitly has a public parameterless instance constructor, it is not possible for a struct type to contain an explicit declaration of a parameterless constructor. 但是允许结构类型声明参数化的实例构造函数 (构造函数)。A struct type is however permitted to declare parameterized instance constructors (Constructors).

结构类型Struct types

结构类型是常量、 字段、 方法、 属性、 索引器、 运算符、 实例构造函数、 静态构造函数和嵌套的类型可以声明的值类型。A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types. 结构类型的声明中所述结构声明The declaration of struct types is described in Struct declarations.

简单类型Simple types

C# 提供了一组预定义的结构类型称为简单类型C# provides a set of predefined struct types called the simple types. 简单类型进行标识通过保留字,但这些保留的字是只需在预定义的结构类型的别名System命名空间下, 表中所述。The simple types are identified through reserved words, but these reserved words are simply aliases for predefined struct types in the System namespace, as described in the table below.

保留的字Reserved word 使用别名类型Aliased type
sbyte System.SByte
byte System.Byte
short System.Int16
ushort System.UInt16
int System.Int32
uint System.UInt32
long System.Int64
ulong System.UInt64
char System.Char
float System.Single
double System.Double
bool System.Boolean
decimal System.Decimal

简单类型的结构类型别名,因为每个简单类型都具有成员。Because a simple type aliases a struct type, every simple type has members. 例如,int具有中声明成员System.Int32和成员继承自System.Object,并允许使用以下语句:For example, int has the members declared in System.Int32 and the members inherited from System.Object, and the following statements are permitted:

int i = int.MaxValue;           // System.Int32.MaxValue constant
string s = i.ToString();        // System.Int32.ToString() instance method
string t = 123.ToString();      // System.Int32.ToString() instance method

简单类型不同于其他结构类型,简单类型允许某些附加操作:The simple types differ from other struct types in that they permit certain additional operations:

  • 大多数简单类型允许值,若要创建的编写文字(文本)。Most simple types permit values to be created by writing literals (Literals). 例如,123是类型的文字int'a'是类型的文本charFor example, 123 is a literal of type int and 'a' is a literal of type char. C# 一般情况下,可以为文本的结构类型没有预配和其他结构类型的非默认值最终始终通过创建这些结构类型的实例构造函数。C# makes no provision for literals of struct types in general, and non-default values of other struct types are ultimately always created through instance constructors of those struct types.
  • 所有简单类型常量表达式的操作数时,就可以为编译器在编译时表达式进行求值。When the operands of an expression are all simple type constants, it is possible for the compiler to evaluate the expression at compile-time. 此类表达式被称为constant_expression (常量表达式)。Such an expression is known as a constant_expression (Constant expressions). 包含由其他结构类型定义运算符的表达式都不是常量表达式。Expressions involving operators defined by other struct types are not considered to be constant expressions.
  • 通过const声明就可以声明的简单类型的常量 (常量)。Through const declarations it is possible to declare constants of the simple types (Constants). 不能具有常量的其他结构类型,但未提供类似的效果static readonly字段。It is not possible to have constants of other struct types, but a similar effect is provided by static readonly fields.
  • 涉及简单类型的转换可以参与的其他结构类型,所定义的转换运算符的计算,但用户定义的转换运算符可以永远不会参与的另一个用户定义运算符的计算 (的评估用户定义的转换)。Conversions involving simple types can participate in evaluation of conversion operators defined by other struct types, but a user-defined conversion operator can never participate in evaluation of another user-defined operator (Evaluation of user-defined conversions).

整型Integral types

C# 支持九个整型类型: sbytebyteshortushortintuintlongulong,和charC# supports nine integral types: sbyte, byte, short, ushort, int, uint, long, ulong, and char. 整型类型具有以下大小和范围的值:The integral types have the following sizes and ranges of values:

  • sbyte类型表示有符号 8 位整数的值介于-128 到 127 之间。The sbyte type represents signed 8-bit integers with values between -128 and 127.
  • byte类型表示其值介于 0 和 255 之间的 8 位无符号的整数。The byte type represents unsigned 8-bit integers with values between 0 and 255.
  • short类型表示有符号 16 位整数值介于-32768 和 32767 之间。The short type represents signed 16-bit integers with values between -32768 and 32767.
  • ushort类型表示无符号的 16 位整数,其值介于 0 和 65535 之间。The ushort type represents unsigned 16-bit integers with values between 0 and 65535.
  • int类型表示有符号 32 位整数值介于-2147483648 到 2147483647 之间。The int type represents signed 32-bit integers with values between -2147483648 and 2147483647.
  • uint类型表示值介于 0 到 4294967295 之间的 32 位无符号的整数。The uint type represents unsigned 32-bit integers with values between 0 and 4294967295.
  • long类型表示有符号 64 位整数的值介于-9223372036854775808 到 9223372036854775807 之间。The long type represents signed 64-bit integers with values between -9223372036854775808 and 9223372036854775807.
  • ulong类型表示值介于 0 到 18446744073709551615 之间的 64 位无符号的整数。The ulong type represents unsigned 64-bit integers with values between 0 and 18446744073709551615.
  • char类型表示无符号的 16 位整数,其值介于 0 和 65535 之间。The char type represents unsigned 16-bit integers with values between 0 and 65535. 可能值的集char类型对应于 Unicode 字符集。The set of possible values for the char type corresponds to the Unicode character set. 尽管char具有相同的表示形式ushort上的其他允许一种类型上允许的不是所有操作。Although char has the same representation as ushort, not all operations permitted on one type are permitted on the other.

整型类型一元和二元运算符总是对有符号的 32 位精度、 无符号的 32 位精度,有符号的 64 位精度,或无符号的 64 位精度操作:The integral-type unary and binary operators always operate with signed 32-bit precision, unsigned 32-bit precision, signed 64-bit precision, or unsigned 64-bit precision:

  • 对于一元+~运算符,操作数将转换为类型T,其中T是第一种intuintlong,和ulong,可以充分表示所有可能的操作数的值。For the unary + and ~ operators, the operand is converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of the operand. 然后使用类型的精度执行该操作T,并且结果的类型为TThe operation is then performed using the precision of type T, and the type of the result is T.
  • 对于一元-运算符,操作数将转换为类型T,其中T是第一种intlong,可以完全表示操作数的所有可能的值。For the unary - operator, the operand is converted to type T, where T is the first of int and long that can fully represent all possible values of the operand. 然后使用类型的精度执行该操作T,并且结果的类型为TThe operation is then performed using the precision of type T, and the type of the result is T. 一元-运算符不能应用于类型的操作数ulongThe unary - operator cannot be applied to operands of type ulong.
  • 二进制文件+-*/%&^|==!=><>=,并<=运算符,操作数转换为类型T,其中T是第一种intuintlong,和ulong,可以完全表示所有可能这两个操作数的值。For the binary +, -, *, /, %, &, ^, |, ==, !=, >, <, >=, and <= operators, the operands are converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of both operands. 然后使用类型的精度执行该操作T,并且结果的类型为T(或bool对于关系运算符)。The operation is then performed using the precision of type T, and the type of the result is T (or bool for the relational operators). 它不允许的一个操作数的类型是long和其他类型ulong使用二元运算符。It is not permitted for one operand to be of type long and the other to be of type ulong with the binary operators.
  • 二进制文件<<>>运算符的左操作数将转换为类型T,其中T是第一种intuintlong,和ulong,可以充分表示所有可能的操作数的值。For the binary << and >> operators, the left operand is converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of the operand. 然后使用类型的精度执行该操作T,并且结果的类型为TThe operation is then performed using the precision of type T, and the type of the result is T.

char类型分类为一种整型类型,但它不同于其他整数类型以两种方式:The char type is classified as an integral type, but it differs from the other integral types in two ways:

  • 不存在隐式转换为其他类型char类型。There are no implicit conversions from other types to the char type. 具体而言,即使sbytebyte,并ushort类型具有的是完全可表示使用的值范围char类型,隐式转换从sbytebyte,或ushortchar不存在。In particular, even though the sbyte, byte, and ushort types have ranges of values that are fully representable using the char type, implicit conversions from sbyte, byte, or ushort to char do not exist.
  • 常数char类型必须写为character_literals 或被integer_literal结合强制转换为类型 s charConstants of the char type must be written as character_literals or as integer_literals in combination with a cast to type char. 例如,(char)10'\x000A' 相同。For example, (char)10 is the same as '\x000A'.

checkedunchecked运算符和语句用于控制溢出检查的整型类型算术运算和转换 (checked 和 unchecked 运算符)。The checked and unchecked operators and statements are used to control overflow checking for integral-type arithmetic operations and conversions (The checked and unchecked operators). 在中checked上下文,溢出会生成编译时错误,或导致System.OverflowException引发。In a checked context, an overflow produces a compile-time error or causes a System.OverflowException to be thrown. unchecked上下文中,将忽略溢出并不适合目标类型中的任何高序位将被丢弃。In an unchecked context, overflows are ignored and any high-order bits that do not fit in the destination type are discarded.

浮点类型Floating point types

C# 支持两个浮点类型:floatdoubleC# supports two floating point types: float and double. floatdouble类型都使用为 32 位单精度和 64 位双精度 IEEE 754 格式提供下列值集来表示:The float and double types are represented using the 32-bit single-precision and 64-bit double-precision IEEE 754 formats, which provide the following sets of values:

  • 正零和负零。Positive zero and negative zero. 在大多数情况下,正零和负零具有相同行为简单的值为零,但某些操作区分两个 (除法运算符)。In most situations, positive zero and negative zero behave identically as the simple value zero, but certain operations distinguish between the two (Division operator).
  • 正无穷大和负无穷大。Positive infinity and negative infinity. 无穷大会生成被零除一个非零数字作为此类操作。Infinities are produced by such operations as dividing a non-zero number by zero. 例如,1.0 / 0.0产生正无穷和-1.0 / 0.0产生负无穷大。For example, 1.0 / 0.0 yields positive infinity, and -1.0 / 0.0 yields negative infinity.
  • Not 数字值,通常缩写的 NaN。The Not-a-Number value, often abbreviated NaN. 无效的浮点运算,如零被零除会生成 Nan。NaNs are produced by invalid floating-point operations, such as dividing zero by zero.
  • 一组有限的窗体的值为非零s * m * 2^e,其中s为 1 或-1,并me取决于特定的浮点类型:有关float0 < m < 2^24-149 <= e <= 104,并为double0 < m < 2^531075 <= e <= 970The finite set of non-zero values of the form s * m * 2^e, where s is 1 or -1, and m and e are determined by the particular floating-point type: For float, 0 < m < 2^24 and -149 <= e <= 104, and for double, 0 < m < 2^53 and 1075 <= e <= 970. 非标准化浮点数被视为有效的非零值。Denormalized floating-point numbers are considered valid non-zero values.

float类型可以表示其值范围从大约1.5 * 10^-453.4 * 10^38且精度为 7 位数字。The float type can represent values ranging from approximately 1.5 * 10^-45 to 3.4 * 10^38 with a precision of 7 digits.

double类型可以表示其值范围从大约5.0 * 10^-3241.7 × 10^308且精度为 15 到 16 位。The double type can represent values ranging from approximately 5.0 * 10^-324 to 1.7 × 10^308 with a precision of 15-16 digits.

如果浮点类型的二元运算符的操作数之一,然后另一个操作数必须是整型或浮点型,并且运算,如下所示:If one of the operands of a binary operator is of a floating-point type, then the other operand must be of an integral type or a floating-point type, and the operation is evaluated as follows:

  • 如果其中一个操作数的整数类型,则该操作数转换为另一个操作数的浮点类型。If one of the operands is of an integral type, then that operand is converted to the floating-point type of the other operand.
  • 然后,如果任一操作数的类型double,另一个操作数转换为double,使用至少执行该操作double范围和精度和结果的类型是double(或bool为关系运算符)。Then, if either of the operands is of type double, the other operand is converted to double, the operation is performed using at least double range and precision, and the type of the result is double (or bool for the relational operators).
  • 否则,该操作将执行使用至少float范围和精度和结果的类型是float(或bool对于关系运算符)。Otherwise, the operation is performed using at least float range and precision, and the type of the result is float (or bool for the relational operators).

浮点运算符,包括赋值运算符,永远不会生成异常。The floating-point operators, including the assignment operators, never produce exceptions. 相反,在异常情况下,浮点运算生成零个、 无穷大或 NaN,如下所述:Instead, in exceptional situations, floating-point operations produce zero, infinity, or NaN, as described below:

  • 如果目标格式太小浮点运算的结果,操作的结果将成为正零或负零。If the result of a floating-point operation is too small for the destination format, the result of the operation becomes positive zero or negative zero.
  • 如果浮点运算的结果太大,目标格式,该操作的结果将成为正无穷或负无穷大。If the result of a floating-point operation is too large for the destination format, the result of the operation becomes positive infinity or negative infinity.
  • 如果浮点运算无效,操作的结果将成为 NaN。If a floating-point operation is invalid, the result of the operation becomes NaN.
  • 如果浮点运算的一个或两个操作数为 NaN,操作的结果将成为 NaN。If one or both operands of a floating-point operation is NaN, the result of the operation becomes NaN.

可能会执行该操作的结果类型比更高精度的浮点运算。Floating-point operations may be performed with higher precision than the result type of the operation. 例如,某些硬件体系结构支持"扩展"或"长双精度"浮点类型,具有更大的范围和精度比double类型,并且隐式地执行所有使用此较高的精度类型的浮点运算。For example, some hardware architectures support an "extended" or "long double" floating-point type with greater range and precision than the double type, and implicitly perform all floating-point operations using this higher precision type. 仅在性能开销过多此类硬件体系结构可用于执行在精度较低,浮点运算,而不是需要实现只好性能和精度,C# 允许更高的精度类型为用于所有浮点运算。Only at excessive cost in performance can such hardware architectures be made to perform floating-point operations with less precision, and rather than require an implementation to forfeit both performance and precision, C# allows a higher precision type to be used for all floating-point operations. 不提供更精确的结果,这很少会有任何显著的影响。Other than delivering more precise results, this rarely has any measurable effects. 但是,在窗体的表达式x * y / z,其中该乘法运算生成结果超出double范围,但后面的除法使临时结果返回到double范围,该表达式是这一事实计算在更高版本范围格式可能会导致有限生成而不是无限的结果。However, in expressions of the form x * y / z, where the multiplication produces a result that is outside the double range, but the subsequent division brings the temporary result back into the double range, the fact that the expression is evaluated in a higher range format may cause a finite result to be produced instead of an infinity.

十进制类型The decimal type

decimal 类型是适用于财务和货币计算的 128 位数据类型。The decimal type is a 128-bit data type suitable for financial and monetary calculations. decimal类型可以表示值介于1.0 * 10^-28到大约7.9 * 10^28具有 28-29 个有效数字。The decimal type can represent values ranging from 1.0 * 10^-28 to approximately 7.9 * 10^28 with 28-29 significant digits.

一组有限的类型的值decimal的形式(-1)^s * c * 10^-e,其中符号s为 0 或 1,决定系数c由给定0 <= *c* < 2^96,和规模e是以便0 <= e <= 28decimal类型不支持签名的零、 无穷大或 NaN 的。The finite set of values of type decimal are of the form (-1)^s * c * 10^-e, where the sign s is 0 or 1, the coefficient c is given by 0 <= *c* < 2^96, and the scale e is such that 0 <= e <= 28.The decimal type does not support signed zeros, infinities, or NaN's. 一个decimal表示为 10 的幂缩放有 96 位整数。A decimal is represented as a 96-bit integer scaled by a power of ten. 有关decimals,使用绝对值小于1.0m,值为精确到 28 位小数,但不要再。For decimals with an absolute value less than 1.0m, the value is exact to the 28th decimal place, but no further. 有关decimal使用绝对值大于或等于 s 1.0m,值为精确到 28 或 29 位数。For decimals with an absolute value greater than or equal to 1.0m, the value is exact to 28 or 29 digits. 到 contraryfloatdouble数据类型,如 0.1 十进制小数数字可表示严格按照decimal表示形式。Contrary to the float and double data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation. 在中floatdouble表示形式,此类数字都是通常无限小,这使得更容易以轮关闭这些表示形式的错误。In the float and double representations, such numbers are often infinite fractions, making those representations more prone to round-off errors.

如果其中一个二元运算符的操作数是类型decimal,则另一个操作数必须是一种整型类型或类型decimalIf one of the operands of a binary operator is of type decimal, then the other operand must be of an integral type or of type decimal. 如果存在整型操作数,则它将转换为decimal执行此操作之前。If an integral type operand is present, it is converted to decimal before the operation is performed.

类型的值的运算的结果decimal是这会导致从计算确切的结果 (保留缩放、 按每个运算符的定义),然后舍入以适合表示形式。The result of an operation on values of type decimal is that which would result from calculating an exact result (preserving scale, as defined for each operator) and then rounding to fit the representation. 结果将舍入为最近的可表示的值,以及当结果同样接近两个可表示值,以最低有效位数位置 (这名为"银行家的舍入") 中具有偶数数目的值。Results are rounded to the nearest representable value, and, when a result is equally close to two representable values, to the value that has an even number in the least significant digit position (this is known as "banker's rounding"). 零个结果始终具有 0 的符号和小数位数为 0。A zero result always has a sign of 0 and a scale of 0.

如果十进制算术运算产生的值小于或等于5 * 10^-29中绝对值的数值,该操作的结果则为零。If a decimal arithmetic operation produces a value less than or equal to 5 * 10^-29 in absolute value, the result of the operation becomes zero. 如果decimal算术运算产生的结果太大decimal格式,System.OverflowException引发。If a decimal arithmetic operation produces a result that is too large for the decimal format, a System.OverflowException is thrown.

decimal类型具有更高的精度比浮点类型的较小范围。The decimal type has greater precision but smaller range than the floating-point types. 因此,对浮点类型转换decimal可能导致溢出异常,以及从转换decimal到浮点类型可能会导致丢失精度。Thus, conversions from the floating-point types to decimal might produce overflow exceptions, and conversions from decimal to the floating-point types might cause loss of precision. 出于这些原因,隐式之间不存在转换浮点类型和decimal,而无需显式强制转换,则可以混合使用浮点和decimal在同一表达式中的操作数。For these reasons, no implicit conversions exist between the floating-point types and decimal, and without explicit casts, it is not possible to mix floating-point and decimal operands in the same expression.

Bool 类型The bool type

bool类型表示布尔逻辑的数量。The bool type represents boolean logical quantities. 可能的值的类型booltruefalseThe possible values of type bool are true and false.

标准之间不存在转换bool和其他类型。No standard conversions exist between bool and other types. 具体而言,bool类型是不同,独立于整型类型和一个bool值不能代替整数值,反之亦然。In particular, the bool type is distinct and separate from the integral types, and a bool value cannot be used in place of an integral value, and vice versa.

C 和C++语言、 零整型或浮点值或 null 指针可以转换为布尔值false,和一个非零的整数或浮点值或非 null 指针可以转换为布尔值true.In the C and C++ languages, a zero integral or floating-point value, or a null pointer can be converted to the boolean value false, and a non-zero integral or floating-point value, or a non-null pointer can be converted to the boolean value true. 在 C# 中,都完成此类转换,通过显式地将整数或浮点数值为零,或通过显式比较的对象引用nullIn C#, such conversions are accomplished by explicitly comparing an integral or floating-point value to zero, or by explicitly comparing an object reference to null.

枚举类型Enumeration types

枚举类型是具有已命名常数的不同类型。An enumeration type is a distinct type with named constants. 每个枚举类型有一个基础类型,它必须是bytesbyteshortushortintuintlongulongEvery enumeration type has an underlying type, which must be byte, sbyte, short, ushort, int, uint, long or ulong. 枚举类型的值集是相同的基础类型的值集。The set of values of the enumeration type is the same as the set of values of the underlying type. 枚举类型的值不限于已命名常数的值。Values of the enumeration type are not restricted to the values of the named constants. 枚举类型定义通过枚举声明 (枚举声明)。Enumeration types are defined through enumeration declarations (Enum declarations).

可以为 null 的类型Nullable types

可以为 null 的类型可以表示的所有值及其基础类型加上其他的 null 值。A nullable type can represent all values of its underlying type plus an additional null value. 可以为 null 的类型被写T?,其中T是基础类型。A nullable type is written T?, where T is the underlying type. 此语法是简写System.Nullable<T>,并且可以互换使用两种形式。This syntax is shorthand for System.Nullable<T>, and the two forms can be used interchangeably.

一个不可以为 null 的值类型相反而不是任意值类型System.Nullable<T>和其简写T?(对于任何T),以及任何类型参数约束为不可为 null 的值类型 (即,任何类型参数与struct约束)。A non-nullable value type conversely is any value type other than System.Nullable<T> and its shorthand T? (for any T), plus any type parameter that is constrained to be a non-nullable value type (that is, any type parameter with a struct constraint). System.Nullable<T>类型指定的值类型约束T(类型参数约束),这意味着可以为 null 的类型的基础类型可以是任何非 null 的值类型。The System.Nullable<T> type specifies the value type constraint for T (Type parameter constraints), which means that the underlying type of a nullable type can be any non-nullable value type. 可以为 null 的类型或引用类型,不能为 null 的类型的基础类型。The underlying type of a nullable type cannot be a nullable type or a reference type. 例如,int??string?是无效的类型。For example, int?? and string? are invalid types.

可以为 null 的类型的实例T?具有两个公共只读属性:An instance of a nullable type T? has two public read-only properties:

  • 一个HasValue类型的属性 boolA HasValue property of type bool
  • 一个Value类型的属性 TA Value property of type T

为其实例HasValue是 true 则称其为非 null。An instance for which HasValue is true is said to be non-null. 非 null 实例包含已知的值和Value返回该值。A non-null instance contains a known value and Value returns that value.

为其实例HasValue是 false 则称其为 null。An instance for which HasValue is false is said to be null. 空实例有未定义的值。A null instance has an undefined value. 尝试读取Value的空实例会导致System.InvalidOperationException引发。Attempting to read the Value of a null instance causes a System.InvalidOperationException to be thrown. 访问的过程Value可以为 null 实例的属性被称为解包The process of accessing the Value property of a nullable instance is referred to as unwrapping.

除了默认构造函数中,每个可以为 null 的类型T?具有一个自变量的类型的公共构造函数TIn addition to the default constructor, every nullable type T? has a public constructor that takes a single argument of type T. 赋值x类型的T,窗体的构造函数调用Given a value x of type T, a constructor invocation of the form

new T?(x)

创建的非 null 实例T?为其Value属性是xcreates a non-null instance of T? for which the Value property is x. 对于给定的值被称为创建可以为 null 的类型的非 null 实例的过程包装The process of creating a non-null instance of a nullable type for a given value is referred to as wrapping.

隐式转换都可通过从null文字T?(Null 文字转换) 和从TT?(可以为 null 的隐式转换).Implicit conversions are available from the null literal to T? (Null literal conversions) and from T to T? (Implicit nullable conversions).

引用类型Reference types

引用类型是类类型、 接口类型、 数组类型或委托类型。A reference type is a class type, an interface type, an array type, or a delegate type.

reference_type
    : class_type
    | interface_type
    | array_type
    | delegate_type
    ;

class_type
    : type_name
    | 'object'
    | 'dynamic'
    | 'string'
    ;

interface_type
    : type_name
    ;

array_type
    : non_array_type rank_specifier+
    ;

non_array_type
    : type
    ;

rank_specifier
    : '[' dim_separator* ']'
    ;

dim_separator
    : ','
    ;

delegate_type
    : type_name
    ;

引用类型值是指实例的类型,后者称为对象A reference type value is a reference to an instance of the type, the latter known as an object. 特殊值null与所有引用类型兼容,并表示实例不存在。The special value null is compatible with all reference types and indicates the absence of an instance.

类类型Class types

类类型定义包含数据成员 (常量和字段),函数成员 (方法、 属性、 事件、 索引器、 运算符、 实例构造函数、 析构函数和静态构造函数) 和嵌套的类型的数据结构。A class type defines a data structure that contains data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types. 类类型支持继承,因此派生的类可以扩展和专门针对基类的机制。Class types support inheritance, a mechanism whereby derived classes can extend and specialize base classes. 使用创建类类型的实例object_creation_expressions (对象创建表达式)。Instances of class types are created using object_creation_expressions (Object creation expressions).

中介绍了类类型Class types are described in Classes.

某些预定义的类类型具有特殊含义,在 C# 语言中下, 表中所述。Certain predefined class types have special meaning in the C# language, as described in the table below.

类类型Class type 说明Description
System.Object 所有其他类型的最终基类。The ultimate base class of all other types. 请参阅的对象类型See The object type.
System.String C# 语言的字符串类型。The string type of the C# language. 请参阅字符串类型See The string type.
System.ValueType 所有值类型的基类。The base class of all value types. 请参阅System.ValueType 类型See The System.ValueType type.
System.Enum 所有枚举类型的基类。The base class of all enum types. 请参阅枚举See Enums.
System.Array 所有数组类型的基类。The base class of all array types. 请参阅数组See Arrays.
System.Delegate 所有委托类型的基类。The base class of all delegate types. 请参阅委托See Delegates.
System.Exception 所有异常类型的基类。The base class of all exception types. 请参阅异常See Exceptions.

对象类型The object type

object类类型是所有其他类型的最基本基类。The object class type is the ultimate base class of all other types. 每个类型在 C# 中的直接或间接派生object类类型。Every type in C# directly or indirectly derives from the object class type.

关键字object是只需为预定义的类的别名System.ObjectThe keyword object is simply an alias for the predefined class System.Object.

动态类型The dynamic type

dynamic类型,如object,可以引用任何其他对象。The dynamic type, like object, can reference any object. 当将运算符应用于类型的表达式dynamic,其解决推迟到运行该程序。When operators are applied to expressions of type dynamic, their resolution is deferred until the program is run. 因此,如果合法不能将运算符应用于所引用对象,会在编译期间发出错误信息。Thus, if the operator cannot legally be applied to the referenced object, no error is given during compilation. 而是运算符的解析在运行时失败时,将引发异常。Instead an exception will be thrown when resolution of the operator fails at run-time.

其目的是允许动态绑定,在详细信息中所述动态绑定Its purpose is to allow dynamic binding, which is described in detail in Dynamic binding.

dynamic 被视为等于object在以下方面除外:dynamic is considered identical to object except in the following respects:

  • 类型的表达式上的操作dynamic可以为动态绑定 (动态绑定)。Operations on expressions of type dynamic can be dynamically bound (Dynamic binding).
  • 类型推理 (类型推理) 会更喜欢dynamic转移object如果两者都是候选项。Type inference (Type inference) will prefer dynamic over object if both are candidates.

由于此等效性,以下包含:Because of this equivalence, the following holds:

  • 没有之间的隐式标识转换objectdynamic,并替换时是相同的构造类型之间dynamicobjectThere is an implicit identity conversion between object and dynamic, and between constructed types that are the same when replacing dynamic with object
  • 之间的隐式和显式转换object还将应用与dynamicImplicit and explicit conversions to and from object also apply to and from dynamic.
  • 替换时是相同的方法签名dynamicobject被视为相同的签名Method signatures that are the same when replacing dynamic with object are considered the same signature
  • 类型dynamic相区别object在运行时。The type dynamic is indistinguishable from object at run-time.
  • 类型的表达式dynamic被称为动态表达式An expression of the type dynamic is referred to as a dynamic expression.

字符串类型The string type

string类型是密封的类类型直接继承objectThe string type is a sealed class type that inherits directly from object. 实例string类表示 Unicode 字符字符串。Instances of the string class represent Unicode character strings.

string类型可以写为字符串文本 (的字符串文本)。Values of the string type can be written as string literals (String literals).

关键字string是只需为预定义的类的别名System.StringThe keyword string is simply an alias for the predefined class System.String.

接口类型Interface types

接口定义一个协定。An interface defines a contract. 类或结构实现的接口必须遵守其协定。A class or struct that implements an interface must adhere to its contract. 一个接口可能从多个基接口继承,类或结构可以实现多个接口。An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.

接口类型所述接口Interface types are described in Interfaces.

数组类型Array types

数组是包含零个或多个可通过计算索引访问的变量的数据结构。An array is a data structure that contains zero or more variables which are accessed through computed indices. 一个数组,也称为数组的元素中包含的变量包括所有相同的类型,而这种类型称为数组的元素类型。The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array.

数组类型中所述数组Array types are described in Arrays.

委托类型Delegate types

委托是指一个或多个方法的数据结构。A delegate is a data structure that refers to one or more methods. 对于实例方法,它还指其对应的对象实例。For instance methods, it also refers to their corresponding object instances.

最接近的委托中 C 或C++为函数指针,但函数指针只能引用静态函数,而委托可以引用静态和实例方法。The closest equivalent of a delegate in C or C++ is a function pointer, but whereas a function pointer can only reference static functions, a delegate can reference both static and instance methods. 在后一种情况下,委托不仅存储存储的引用方法的入口点,还对在其上调用该方法的对象实例的引用。In the latter case, the delegate stores not only a reference to the method's entry point, but also a reference to the object instance on which to invoke the method.

中介绍了委托类型委托Delegate types are described in Delegates.

装箱和取消装箱Boxing and unboxing

装箱和取消装箱的概念是 C# 类型系统的核心。The concept of boxing and unboxing is central to C#'s type system. 它提供了之间的桥梁value_types 和reference_type允许存在的任何值的 s value_type要转换到和从类型objectIt provides a bridge between value_types and reference_types by permitting any value of a value_type to be converted to and from type object. 装箱和取消装箱使类型系统,其中任何类型的值最终都可以作为对象处理的统一的视图。Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately be treated as an object.

装箱转换Boxing conversions

装箱转换允许value_type隐式转换为reference_typeA boxing conversion permits a value_type to be implicitly converted to a reference_type. 存在以下装箱转换:The following boxing conversions exist:

  • 从任何value_type为类型objectFrom any value_type to the type object.
  • 从任何value_type为类型System.ValueTypeFrom any value_type to the type System.ValueType.
  • 从任何non_nullable_value_type任何interface_type由实现value_typeFrom any non_nullable_value_type to any interface_type implemented by the value_type.
  • 从任何nullable_type任何interface_type实现的基础类型的nullable_typeFrom any nullable_type to any interface_type implemented by the underlying type of the nullable_type.
  • 从任何enum_type为类型System.EnumFrom any enum_type to the type System.Enum.
  • 从任何nullable_type与基础enum_type为类型System.EnumFrom any nullable_type with an underlying enum_type to the type System.Enum.
  • 请注意从类型参数的隐式转换将执行作为装箱转换中,是否在运行时其结果是从值类型转换为引用类型 (涉及类型参数的隐式转换)。Note that an implicit conversion from a type parameter will be executed as a boxing conversion if at run-time it ends up converting from a value type to a reference type (Implicit conversions involving type parameters).

装箱的值non_nullable_value_type分配给对象实例和复制组成non_nullable_value_type到该实例的值。Boxing a value of a non_nullable_value_type consists of allocating an object instance and copying the non_nullable_value_type value into that instance.

装箱的值nullable_type生成一个空引用,它是否null值 (HasValuefalse),或解包和否则装箱的基础值的结果。Boxing a value of a nullable_type produces a null reference if it is the null value (HasValue is false), or the result of unwrapping and boxing the underlying value otherwise.

装箱的值的实际过程non_nullable_value_type很好地解释由专门的泛型是否存在装箱类,其行为就像它已声明,如下所示:The actual process of boxing a value of a non_nullable_value_type is best explained by imagining the existence of a generic boxing class, which behaves as if it were declared as follows:

sealed class Box<T>: System.ValueType
{
    T value;

    public Box(T t) {
        value = t;
    }
}

值的装箱v类型的T现在包含执行表达式new Box<T>(v),并返回生成的实例类型的值作为objectBoxing of a value v of type T now consists of executing the expression new Box<T>(v), and returning the resulting instance as a value of type object. 因此,下面的语句Thus, the statements

int i = 123;
object box = i;

从概念上讲对应于conceptually correspond to

int i = 123;
object box = new Box<int>(i);

装箱类,如Box<T>更高版本并不实际存在和装箱值的动态类型不是实际的类类型。A boxing class like Box<T> above doesn't actually exist and the dynamic type of a boxed value isn't actually a class type. 相反,类型的装箱的值T具有动态类型T,和动态类型检查使用is运算符只是引用类型TInstead, a boxed value of type T has the dynamic type T, and a dynamic type check using the is operator can simply reference type T. 例如,应用于对象的For example,

int i = 123;
object box = i;
if (box is int) {
    Console.Write("Box contains an int");
}

将输出字符串"Box contains an int"控制台上。will output the string "Box contains an int" on the console.

装箱转换隐含着一份待装箱的值。A boxing conversion implies making a copy of the value being boxed. 这与不同的转换reference_type键入object,将继续引用同一个实例以及仅被视为派生程度更小的类型值中objectThis is different from a conversion of a reference_type to type object, in which the value continues to reference the same instance and simply is regarded as the less derived type object. 例如,给定声明For example, given the declaration

struct Point
{
    public int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

下面的语句the following statements

Point p = new Point(10, 10);
object box = p;
p.x = 20;
Console.Write(((Point)box).x);

将输出在控制台上的值 10,因为发生的分配中的隐式装箱操作pbox的值将导致p要复制。will output the value 10 on the console because the implicit boxing operation that occurs in the assignment of p to box causes the value of p to be copied. Point已声明class相反,值 20 会是输出,因为pbox将引用同一个实例。Had Point been declared a class instead, the value 20 would be output because p and box would reference the same instance.

取消装箱转换Unboxing conversions

取消装箱转换允许reference_type显式转换为value_typeAn unboxing conversion permits a reference_type to be explicitly converted to a value_type. 存在以下取消装箱转换:The following unboxing conversions exist:

  • 从类型object到任何value_typeFrom the type object to any value_type.
  • 从类型System.ValueType到任何value_typeFrom the type System.ValueType to any value_type.
  • 从任何interface_type任何non_nullable_value_type实现interface_typeFrom any interface_type to any non_nullable_value_type that implements the interface_type.
  • 从任何interface_type任何nullable_type其基础类型实现interface_typeFrom any interface_type to any nullable_type whose underlying type implements the interface_type.
  • 从类型System.Enum到任何enum_typeFrom the type System.Enum to any enum_type.
  • 从类型System.Enum到任何nullable_type与基础enum_typeFrom the type System.Enum to any nullable_type with an underlying enum_type.
  • 请注意类型参数的显式转换将执行作为取消装箱转换中,是否在运行时其结果是从引用类型转换为值类型 (显式动态转换)。Note that an explicit conversion to a type parameter will be executed as an unboxing conversion if at run-time it ends up converting from a reference type to a value type (Explicit dynamic conversions).

取消装箱操作向non_nullable_value_type组成的对象实例的装箱的值的第一个检查给定non_nullable_value_type,然后复制出的值实例。An unboxing operation to a non_nullable_value_type consists of first checking that the object instance is a boxed value of the given non_nullable_value_type, and then copying the value out of the instance.

到取消装箱nullable_type生成的 null 值nullable_type如果源操作数为null,或取消装箱的基础类型的对象实例的已包装的结果nullable_type否则为。Unboxing to a nullable_type produces the null value of the nullable_type if the source operand is null, or the wrapped result of unboxing the object instance to the underlying type of the nullable_type otherwise.

引用在上一部分中,取消装箱转换的对象中所述的虚部装箱类boxvalue_type T组成执行表达式((Box<T>)box).valueReferring to the imaginary boxing class described in the previous section, an unboxing conversion of an object box to a value_type T consists of executing the expression ((Box<T>)box).value. 因此,下面的语句Thus, the statements

object box = 123;
int i = (int)box;

从概念上讲对应于conceptually correspond to

object box = new Box<int>(123);
int i = ((Box<int>)box).value;

取消装箱转换为给定non_nullable_value_type若要成功运行时,源操作数的值必须是对已装箱值,该值的引用non_nullable_value_typeFor an unboxing conversion to a given non_nullable_value_type to succeed at run-time, the value of the source operand must be a reference to a boxed value of that non_nullable_value_type. 如果源操作数nullSystem.NullReferenceException引发。If the source operand is null, a System.NullReferenceException is thrown. 如果源操作数是一个不兼容的对象的引用System.InvalidCastException引发。If the source operand is a reference to an incompatible object, a System.InvalidCastException is thrown.

取消装箱转换为给定nullable_type若要成功运行时,源操作数的值必须是null或对基础的装箱值的引用non_nullable_value_typenullable_typeFor an unboxing conversion to a given nullable_type to succeed at run-time, the value of the source operand must be either null or a reference to a boxed value of the underlying non_nullable_value_type of the nullable_type. 如果源操作数是一个不兼容的对象的引用System.InvalidCastException引发。If the source operand is a reference to an incompatible object, a System.InvalidCastException is thrown.

构造的类型Constructed types

泛型类型声明,其本身而言,表示未绑定的泛型类型用作"蓝图"以形成许多不同类型,通过应用类型参数A generic type declaration, by itself, denotes an unbound generic type that is used as a "blueprint" to form many different types, by way of applying type arguments. 类型实参编写在尖括号 (<>) 紧跟泛型类型的名称。The type arguments are written within angle brackets (< and >) immediately following the name of the generic type. 包含至少一个类型参数的类型称为构造类型A type that includes at least one type argument is called a constructed type. 可在类型名称可以在其中显示的语言中的大多数位置构造的类型。A constructed type can be used in most places in the language in which a type name can appear. 未绑定的泛型类型仅可在typeof_expression (typeof 运算符)。An unbound generic type can only be used within a typeof_expression (The typeof operator).

构造的类型还可在表达式中作为简单名称 (的简单名称) 或访问成员时 (成员访问)。Constructed types can also be used in expressions as simple names (Simple names) or when accessing a member (Member access).

namespace_or_type_name是使用正确的类型参数被视为数计算,仅泛型类型。When a namespace_or_type_name is evaluated, only generic types with the correct number of type parameters are considered. 因此,它是可以使用相同的标识符来标识不同类型,只要类型具有不同数量的类型参数。Thus, it is possible to use the same identifier to identify different types, as long as the types have different numbers of type parameters. 混合在同一程序中的泛型和非泛型类时,这非常有用:This is useful when mixing generic and non-generic classes in the same program:

namespace Widgets
{
    class Queue {...}
    class Queue<TElement> {...}
}

namespace MyApplication
{
    using Widgets;

    class X
    {
        Queue q1;            // Non-generic Widgets.Queue
        Queue<int> q2;       // Generic Widgets.Queue
    }
}

一个type_name可能会标识构造的类型,即使它不直接指定类型参数。A type_name might identify a constructed type even though it doesn't specify type parameters directly. 这可能是其中一种类型嵌套在泛型类声明中,并且包含声明的实例类型隐式用于名称查找 (嵌套在泛型类类型):This can occur where a type is nested within a generic class declaration, and the instance type of the containing declaration is implicitly used for name lookup (Nested types in generic classes):

class Outer<T>
{
    public class Inner {...}

    public Inner i;                // Type of i is Outer<T>.Inner
}

不安全代码中构造的类型不能用作unmanaged_type (指针类型)。In unsafe code, a constructed type cannot be used as an unmanaged_type (Pointer types).

类型参数Type arguments

类型实参列表中的每个参数是只需类型Each argument in a type argument list is simply a type.

type_argument_list
    : '<' type_arguments '>'
    ;

type_arguments
    : type_argument (',' type_argument)*
    ;

type_argument
    : type
    ;

在不安全代码 (不安全代码)、 一个type_argument可能不是指针类型。In unsafe code (Unsafe code), a type_argument may not be a pointer type. 每个类型实参必须满足在相应的所有约束都类型参数 (都类型参数约束)。Each type argument must satisfy any constraints on the corresponding type parameter (Type parameter constraints).

打开和关闭类型Open and closed types

可以为所有类型进行都分类开放类型封闭类型All types can be classified as either open types or closed types. 开放类型是涉及类型参数的类型。An open type is a type that involves type parameters. 具体而言:More specifically:

  • 类型参数定义开放类型。A type parameter defines an open type.
  • 数组类型是开放类型,当且仅当其元素类型是开放类型。An array type is an open type if and only if its element type is an open type.
  • 构造的类型是开放类型,当且仅当一个或多个其类型参数是开放类型。A constructed type is an open type if and only if one or more of its type arguments is an open type. 当且仅当一个或多个其类型参数或其包含类型的类型参数是开放类型,构造的嵌套的类型是开放类型。A constructed nested type is an open type if and only if one or more of its type arguments or the type arguments of its containing type(s) is an open type.

关闭的类型是不是开放类型的类型。A closed type is a type that is not an open type.

在运行时,所有的泛型类型声明中的代码是通过将类型参数应用于泛型声明创建封闭式构造类型的上下文中执行。At run-time, all of the code within a generic type declaration is executed in the context of a closed constructed type that was created by applying type arguments to the generic declaration. 泛型类型中的每个类型参数是绑定到特定的运行时类型。Each type parameter within the generic type is bound to a particular run-time type. 运行时处理的所有语句和表达式始终是使用封闭类型,并打开类型仅出现在编译时处理。The run-time processing of all statements and expressions always occurs with closed types, and open types occur only during compile-time processing.

每个封闭的构造的类型都有自己的静态变量,不与任何其他封闭式构造类型共享的一组。Each closed constructed type has its own set of static variables, which are not shared with any other closed constructed types. 在运行时,打开类型不存在,因为没有静态变量与开放类型相关联。Since an open type does not exist at run-time, there are no static variables associated with an open type. 如果它们从同一个未绑定泛型类型,构造和其相应的类型参数具有相同的类型,则两个封闭式构造的类型具有相同的类型。Two closed constructed types are the same type if they are constructed from the same unbound generic type, and their corresponding type arguments are the same type.

绑定和未绑定类型Bound and unbound types

术语未绑定类型指非泛型类型或未绑定的泛型类型。The term unbound type refers to a non-generic type or an unbound generic type. 术语绑定类型指非泛型类型或构造的类型。The term bound type refers to a non-generic type or a constructed type.

未绑定的类型是指声明的类型声明的实体。An unbound type refers to the entity declared by a type declaration. 未绑定的泛型类型本身不是一种类型,并为变量、 参数或返回值的类型或基类型不能使用。An unbound generic type is not itself a type, and cannot be used as the type of a variable, argument or return value, or as a base type. 可以在其中引用未绑定的泛型类型的唯一构造是typeof表达式 (typeof 运算符)。The only construct in which an unbound generic type can be referenced is the typeof expression (The typeof operator).

令人满意的约束Satisfying constraints

每当引用构造的类型或泛型方法时,都会根据泛型类型或方法上声明的类型参数约束检查提供的类型实参 (类型参数约束)。Whenever a constructed type or generic method is referenced, the supplied type arguments are checked against the type parameter constraints declared on the generic type or method (Type parameter constraints). 每个where子句中,类型参数A对应于已命名类型参数检查针对每个约束,如下所示:For each where clause, the type argument A that corresponds to the named type parameter is checked against each constraint as follows:

  • 如果约束为类类型、 接口类型或类型参数,则假设C表示具有提供的类型参数约束替换为该约束中出现的任何类型参数。If the constraint is a class type, an interface type, or a type parameter, let C represent that constraint with the supplied type arguments substituted for any type parameters that appear in the constraint. 若要满足的约束,它必须是类型的用例A可转换为类型C由以下项之一:To satisfy the constraint, it must be the case that type A is convertible to type C by one of the following:
  • 如果约束是引用类型约束 (class),该类型A必须满足以下项之一:If the constraint is the reference type constraint (class), the type A must satisfy one of the following:
    • A 接口类型、 类类型、 委托类型或数组类型。A is an interface type, class type, delegate type or array type. 请注意,System.ValueTypeSystem.Enum是满足此约束的引用类型。Note that System.ValueType and System.Enum are reference types that satisfy this constraint.
    • A 是一个已知为引用类型的类型参数 (类型参数约束)。A is a type parameter that is known to be a reference type (Type parameter constraints).
  • 如果约束是值类型约束 (struct),该类型A必须满足以下项之一:If the constraint is the value type constraint (struct), the type A must satisfy one of the following:
    • A 为结构类型或枚举类型,但不是为 null 的类型。A is a struct type or enum type, but not a nullable type. 请注意,System.ValueTypeSystem.Enum是引用类型,不满足此约束。Note that System.ValueType and System.Enum are reference types that do not satisfy this constraint.
    • A 为具有值类型约束的类型形参 (类型参数约束)。A is a type parameter having the value type constraint (Type parameter constraints).
  • 如果约束是构造函数约束new(),类型A不能abstract并且必须具有公共无参数构造函数。If the constraint is the constructor constraint new(), the type A must not be abstract and must have a public parameterless constructor. 如果以下项之一为 true,条件则满足此问题:This is satisfied if one of the following is true:
    • A 为值类型,因为所有值类型都具有公共默认构造函数 (默认构造函数)。A is a value type, since all value types have a public default constructor (Default constructors).
    • A 为具有构造函数约束的类型形参 (类型参数约束)。A is a type parameter having the constructor constraint (Type parameter constraints).
    • A 为具有值类型约束的类型形参 (类型参数约束)。A is a type parameter having the value type constraint (Type parameter constraints).
    • A 是一个类,不是abstract和包含显式声明public不带任何参数的构造函数。A is a class that is not abstract and contains an explicitly declared public constructor with no parameters.
    • A 不是abstract并具有默认构造函数 (默认构造函数)。A is not abstract and has a default constructor (Default constructors).

如果按给定的类型参数不满足一个或多个类型形参的约束,则,发生编译时错误。A compile-time error occurs if one or more of a type parameter's constraints are not satisfied by the given type arguments.

约束类型参数不继承的因为是永远不会继承任何一个。Since type parameters are not inherited, constraints are never inherited either. 在以下示例中,D需要指定其类型形参上的约束T以便T满足所施加的类的基类约束B<T>In the example below, D needs to specify the constraint on its type parameter T so that T satisfies the constraint imposed by the base class B<T>. 与此相反,类E不需要指定一个约束,因为List<T>实现IEnumerable任何TIn contrast, class E need not specify a constraint, because List<T> implements IEnumerable for any T.

class B<T> where T: IEnumerable {...}

class D<T>: B<T> where T: IEnumerable {...}

class E<T>: B<List<T>> {...}

类型参数Type parameters

类型参数是值类型或引用类型的参数在运行时绑定到指定的标识符。A type parameter is an identifier designating a value type or reference type that the parameter is bound to at run-time.

type_parameter
    : identifier
    ;

由于类型参数可以具有多个不同的实际类型参数实例化,类型参数具有与其他类型稍有不同的操作和限制。Since a type parameter can be instantiated with many different actual type arguments, type parameters have slightly different operations and restrictions than other types. 这些问题包括:These include:

作为一种类型,类型参数都是纯粹是一个编译时构造。As a type, type parameters are purely a compile-time construct. 在运行时,每个类型参数绑定到提供的泛型类型声明的类型参数指定的运行时类型。At run-time, each type parameter is bound to a run-time type that was specified by supplying a type argument to the generic type declaration. 因此,使用类型参数将在运行时,声明一个变量的类型为封闭式构造的类型 (打开和关闭类型)。Thus, the type of a variable declared with a type parameter will, at run-time, be a closed constructed type (Open and closed types). 运行时执行的所有语句和包含类型参数的表达式使用提供的实际类型作为该参数的类型参数。The run-time execution of all statements and expressions involving type parameters uses the actual type that was supplied as the type argument for that parameter.

表达式树类型Expression tree types

表达式树允许 lambda 表达式,表示为数据结构,而不是可执行代码。Expression trees permit lambda expressions to be represented as data structures instead of executable code. 表达式树是值表达式树类型窗体System.Linq.Expressions.Expression<D>,其中D是任何委托类型。Expression trees are values of expression tree types of the form System.Linq.Expressions.Expression<D>, where D is any delegate type. 此规范的剩余时间内我们将把这些类型使用速记Expression<D>For the remainder of this specification we will refer to these types using the shorthand Expression<D>.

如果转换为委托类型从 lambda 表达式存在D,转换为表达式树类型也存在于Expression<D>If a conversion exists from a lambda expression to a delegate type D, a conversion also exists to the expression tree type Expression<D>. 为委托类型的 lambda 表达式转换生成引用 lambda 表达式的可执行代码的委托,而转换为表达式树类型创建 lambda 表达式的表达式树表示形式。Whereas the conversion of a lambda expression to a delegate type generates a delegate that references executable code for the lambda expression, conversion to an expression tree type creates an expression tree representation of the lambda expression.

表达式树的 lambda 表达式的高效内存中数据表示形式,并使 lambda 表达式结构的透明和显式。Expression trees are efficient in-memory data representations of lambda expressions and make the structure of the lambda expression transparent and explicit.

就像是委托类型DExpression<D>称其具有参数和返回类型,它们是相同的DJust like a delegate type D, Expression<D> is said to have parameter and return types, which are the same as those of D.

下面的示例表示 lambda 表达式作为可执行代码和表达式树。The following example represents a lambda expression both as executable code and as an expression tree. 由于存在到转换Func<int,int>,转换也存在到Expression<Func<int,int>>:Because a conversion exists to Func<int,int>, a conversion also exists to Expression<Func<int,int>>:

Func<int,int> del = x => x + 1;                    // Code

Expression<Func<int,int>> exp = x => x + 1;        // Data

遵循这些分配委托del引用返回的方法x + 1,并在表达式树exp引用描述表达式的数据结构x => x + 1Following these assignments, the delegate del references a method that returns x + 1, and the expression tree exp references a data structure that describes the expression x => x + 1.

泛型类型的精确定义Expression<D>构造表达式树 lambda 表达式转换为表达式目录树类型时的精确规则,以及将此规范的范围之内。The exact definition of the generic type Expression<D> as well as the precise rules for constructing an expression tree when a lambda expression is converted to an expression tree type, are both outside the scope of this specification.

以下两种情况非常重要明确:Two things are important to make explicit:

  • 并非所有的 lambda 表达式可以转换为表达式树。Not all lambda expressions can be converted to expression trees. 例如,不能表示具有语句体的 lambda 表达式和其中包含赋值表达式的 lambda 表达式。For instance, lambda expressions with statement bodies, and lambda expressions containing assignment expressions cannot be represented. 在这些情况下,转换仍存在,但将在编译时失败。In these cases, a conversion still exists, but will fail at compile-time. 中详细介绍了这些异常匿名函数转换These exceptions are detailed in Anonymous function conversions.

  • Expression<D> 提供的实例方法Compile可生成类型的委托D:Expression<D> offers an instance method Compile which produces a delegate of type D:

    Func<int,int> del2 = exp.Compile();
    

    调用此委托会导致要执行表达式树所表示的代码。Invoking this delegate causes the code represented by the expression tree to be executed. 因此,给定上述定义后,del 和 del2 等效的并且在以下两个语句都将具有相同的效果:Thus, given the definitions above, del and del2 are equivalent, and the following two statements will have the same effect:

    int i1 = del(1);
    
    int i2 = del2(1);
    

    执行此代码后i1i2都将拥有值2After executing this code, i1 and i2 will both have the value 2.