Dela via


int (C# Reference)

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

Literals

You can declare and initialize a variable of the type int like this example:

int i = 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.

Conversions

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;

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.

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.

For more information on arithmetic expressions with mixed floating-point types and integral types, see float and double.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.3 Types and Variables

  • 4.1.5 Integral Types

See Also

Concepts

C# Programming Guide

Reference

C# Keywords

Integral Types Table (C# Reference)

Built-In Types Table (C# Reference)

Implicit Numeric Conversions Table (C# Reference)

Explicit Numeric Conversions Table (C# Reference)

Int32

Other Resources

C# Reference