Skip to content

Latest commit

 

History

History
90 lines (68 loc) · 4.88 KB

File metadata and controls

90 lines (68 loc) · 4.88 KB
title int (C# Reference)
ms.date 03/14/2017
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
f1_keywords
int_CSharpKeyword
int
helpviewer_keywords
int keyword [C#]
ms.assetid 212447b4-5d2a-41aa-88ab-84fe710bdb52
caps.latest.revision 19
author BillWagner
ms.author wiwagn

int (C# Reference)

int denotes an integral type that stores values according to the size and range shown in the following table.

Type Range Size .NET Framework type Default Value
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer xref:System.Int32?displayProperty=nameWithType 0

Literals

You can declare and initialize an int variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7) a binary literal to it. If the integer literal is outside the range of int (that is, if it is less than xref:System.Int32.MinValue?displayProperty=nameWithType or greater than xref:System.Int32.MaxValue?displayProperty=nameWithType), a compilation error occurs.

In the following example, integers equal to 90,946 that are represented as decimal, hexadecimal, and binary literals are assigned to int values.

[!code-csharpint]

Note

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.

Starting with C# 7, a couple of features have been added to enhance readability.

  • C# 7.0 allows the usage of the underscore character, _, as a digit separator.
  • 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.

[!code-csharpint]

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. uint
  3. long
  4. ulong

In these examples, the literal 90946 is of 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

[!INCLUDECSharplangspec]

See Also

xref:System.Int32
C# Reference
C# Programming Guide
C# Keywords
Integral Types Table
Built-In Types Table
Implicit Numeric Conversions Table
Explicit Numeric Conversions Table