|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
The int keyword denotes an integral type that stores values according to the size and range shown in the following table.
|Type||Range||Size||.NET Framework type|
|int||-2,147,483,648 to 2,147,483,647||Signed 32-bit integer||System.Int32|
You can declare and initialize a variable of the type int like this example:
int myInt = 123;
When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. In this example, it is of the type int.
There is a predefined implicit conversion from int to long, float, double, or decimal. For example:
float myFloat = 123; // OK: implicit conversion to float
There is a predefined implicit conversion from sbyte, byte, short, ushort, or char to int. For example, if you have a long variable,
myLong, the following assignment statement will produce a compilation error without a cast:
long myLong = 22; int myInt = myLong; // Error: no implicit conversion from long int myInt = (int)myLong; // OK: explicit conversion
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