int(C# 参考)int (C# Reference)

int 表示一种整型类型,该类型根据下表显示的大小和范围存储值。int denotes an integral type that stores values according to the size and range shown in the following table.

类型Type 范围Range 大小Size .NET 类型.NET type
int -2,147,483,648 到 2,147,483,647-2,147,483,648 to 2,147,483,647 带符号的 32 位整数Signed 32-bit integer System.Int32


可以通过为其分配十进制文本、十六进制文本或(从 C# 7.0 开始)二进制文本来声明和初始化 int 变量。You can declare and initialize an int variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7.0) a binary literal to it. 如果整数文本在 int 范围之外(即,如果它小于 Int32.MinValue 或大于 Int32.MaxValue),会发生编译错误。If the integer literal is outside the range of int (that is, if it is less than Int32.MinValue or greater than Int32.MaxValue), a compilation error occurs.

在以下示例中,表示为十进制、十六进制和二进制文本且等于 90,946 的整数被分配给 int 值。In the following example, integers equal to 90,946 that are represented as decimal, hexadecimal, and binary literals are assigned to int values.

int intValue1 = 90946;
int intValue2 = 0x16342;

int intValue3 = 0b0001_0110_0011_0100_0010;
// The example displays the following output:
//          90946
//          90946
//          90946


使用前缀 0x0X 表示十六进制文本,使用前缀 0b0B 表示二进制文本。You use the prefix 0x or 0X to denote a hexadecimal literal and the prefix 0b or 0B to denote a binary literal. 十进制文本没有前缀。Decimal literals have no prefix.

从 C# 7.0 开始,添加了一些功能以增强可读性。Starting with C# 7.0, a couple of features have been added to enhance readability.

  • C# 7.0 允许将下划线字符 (_) 用作数字分隔符。C# 7.0 allows the usage of the underscore character, _, as a digit separator.
  • C# 7.2 允许将 _ 用作二进制或十六进制文本的数字分隔符,位于前缀之后。C# 7.2 allows _ to be used as a digit separator for a binary or hexadecimal literal, after the prefix. 十进制文本不能够有前导下划线。A decimal literal isn't permitted to have a leading underscore.

下面是一些示例。Some examples are shown below.

int intValue1 = 90_946;

int intValue2 = 0x0001_6342;

int intValue3 = 0b0001_0110_0011_0100_0010;

int intValue4 = 0x_0001_6342;       // C# 7.2 onwards

int intValue5 = 0b_0001_0110_0011_0100_0010;       // C# 7.2 onwards
// The example displays the following output:
//          90946
//          90946
//          90946
//          90946
//          90946

整数文本还可以包括一个表示类型的后缀,尽管没有表示 int 类型的后缀。Integer literals can also include a suffix that denotes the type, although there is no suffix that denotes the int type. 如果整数文本没有后缀,则其类型为以下类型中可表示其值的第一个类型:If an integer literal has no suffix, its type is the first of the following types in which its value can be represented:

  1. int
  2. uintuint
  3. longlong
  4. ulongulong

在这些示例中,文本 90946 属于类型 intIn these examples, the literal 90946 is of type int.


存在从 intlongfloatdoubledecimal 的预定义隐式转换。There is a predefined implicit conversion from int to long, float, double, or decimal. 例如:For example:

// '123' is an int, so an implicit conversion takes place here:
float f = 123;

存在从 sbytebyteshortushortcharint 的预定义隐式转换。There is a predefined implicit conversion from sbyte, byte, short, ushort, or char to int. 例如,如果不使用转换,以下赋值语句会生成编译错误:For example, the following assignment statement will produce a compilation error without a cast:

long aLong = 22;
int i1 = aLong;       // Error: no implicit conversion from long.
int i2 = (int)aLong;  // OK: explicit conversion.

另请注意,不存在从浮点类型到 int 类型的隐式转换。Notice also that there is no implicit conversion from floating-point types to int. 例如,除非使用显式强制转换,否则以下语句将生成编译器错误:For example, the following statement generates a compiler error unless an explicit cast is used:

int x = 3.0;         // Error: no implicit conversion from double.
int y = (int)3.0;    // OK: explicit conversion.

有关兼用浮点类型和整型类型的算术表达式的详细信息,请参阅 floatdoubleFor more information on arithmetic expressions with mixed floating-point types and integral types, see float and double.

C# 语言规范C# Language Specification

有关详细信息,请参阅 C# 语言规范中的整型类型For more information, see Integral types in the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also