int(C# 参考)

int 关键字表示一种整型,该类型根据下表显示的大小和范围存储值。

Type

范围

大小

.NET Framework 类型

int

-2,147,483,648 到 2,147,483,647

有符号 32 位整数

System.Int32

文本

可以声明并初始化 int 类型的变量,例如:

int i = 123;

如果整数没有后缀,则其类型为以下类型中可表示其值的第一个类型:int、uintlongulong。 在此例中为 int 类型。

转换

存在从 int 到 longfloatdoubledecimal 的预定义隐式转换。 例如:

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

存在从 sbytebyteshortushortchar 到 int 的预定义隐式转换。 例如,如果不进行强制转换,下面的赋值语句将产生编译错误:

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

还请注意,不存在从浮点型到 int 类型的隐式转换。 例如,除非使用显式强制转换,否则以下语句将生成一个编译器错误:

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

有关兼用浮点型和整型的算术表达式的更多信息,请参见 floatdouble

C# 语言规范

有关更多信息,请参见 C# 语言规范。C# 语言规范是 C# 语法和用法的权威资料。

请参见

参考

C# 关键字

整型表(C# 参考)

内置类型表(C# 参考)

隐式数值转换表(C# 参考)

显式数值转换表(C# 参考)

Int32

概念

C# 编程指南

其他资源

C# 参考