整型数值类型(C# 参考)Integral numeric types (C# reference)

整型数值类型 表示整数。The integral numeric types represent integer numbers. 所有的整型数值类型均为值类型All integral numeric types are value types. 它们还是简单类型,可以使用文本进行初始化。They are also simple types and can be initialized with literals. 所有整型数值类型都支持算术位逻辑比较相等运算符。All integral numeric types support arithmetic, bitwise logical, comparison, and equality operators.

整型类型的特征Characteristics of the integral types

C# 支持以下预定义整型类型:C# supports the following predefined integral types:

C# 类型/关键字C# type/keyword 范围Range 大小Size .NET 类型.NET type
sbyte -128 到 127-128 to 127 8 位带符号整数Signed 8-bit integer System.SByte
byte 0 到 2550 to 255 无符号的 8 位整数Unsigned 8-bit integer System.Byte
short -32,768 到 32,767-32,768 to 32,767 有符号 16 位整数Signed 16-bit integer System.Int16
ushort 0 到 65,5350 to 65,535 无符号 16 位整数Unsigned 16-bit integer System.UInt16
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
uint 0 到 4,294,967,2950 to 4,294,967,295 无符号的 32 位整数Unsigned 32-bit integer System.UInt32
long -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 64 位带符号整数Signed 64-bit integer System.Int64
ulong 0 到 18,446,744,073,709,551,6150 to 18,446,744,073,709,551,615 无符号 64 位整数Unsigned 64-bit integer System.UInt64

在上表中,最左侧列中的每个 C# 类型关键字都是相应 .NET 类型的别名。In the preceding table, each C# type keyword from the leftmost column is an alias for the corresponding .NET type. 它们是可互换的。They are interchangeable. 例如,以下声明声明了相同类型的变量:For example, the following declarations declare variables of the same type:

int a = 123;
System.Int32 b = 123;

每个整型类型的默认值都为零 0The default value of each integral type is zero, 0. 每个整型类型都有 MinValueMaxValue 常量,提供该类型的最小值和最大值。Each of the integral types has the MinValue and MaxValue constants that provide the minimum and maximum value of that type.

System.Numerics.BigInteger 结构用于表示没有上限或下限的带符号整数。Use the System.Numerics.BigInteger structure to represent a signed integer with no upper or lower bounds.

整数文本Integer literals

整数文本可以是Integer literals can be

  • 十进制:不使用任何前缀decimal: without any prefix
  • 十六进制:使用 0x0X 前缀hexadecimal: with the 0x or 0X prefix
  • 二进制:使用 0b0B 前缀(在 C# 7.0 和更高版本中可用)binary: with the 0b or 0B prefix (available in C# 7.0 and later)

下面的代码演示每种类型的示例:The following code demonstrates an example of each:

var decimalLiteral = 42;
var hexLiteral = 0x2A;
var binaryLiteral = 0b_0010_1010;

前面的示例还演示了如何将 _ 用作数字分隔符 (从 C# 7.0 开始提供支持)。The preceding example also shows the use of _ as a digit separator, which is supported starting with C# 7.0. 可以将数字分隔符用于所有类型的数字文本。You can use the digit separator with all kinds of numeric literals.

整数文本的类型由其后缀确定,如下所示:The type of an integer literal is determined by its suffix as follows:

  • 如果文本没有后缀,则其类型为以下类型中可表示其值的第一个类型:intuintlongulongIf the literal has no suffix, its type is the first of the following types in which its value can be represented: int, uint, long, ulong.

  • 如果文本以 Uu 为后缀,则其类型为以下类型中可表示其值的第一个类型:uintulongIf the literal is suffixed by U or u, its type is the first of the following types in which its value can be represented: uint, ulong.

  • 如果文本以 Ll 为后缀,则其类型为以下类型中可表示其值的第一个类型:longulongIf the literal is suffixed by L or l, its type is the first of the following types in which its value can be represented: long, ulong.

    备注

    可以使用小写字母 l 作为后缀。You can use the lowercase letter l as a suffix. 但是,这会生成一个编译器警告,因为字母 l 可能与数字 1 混淆。However, this generates a compiler warning because the letter l can be confused with the digit 1. 为清楚起见,请使用 LUse L for clarity.

  • 如果文本的后缀为 ULUluLulLULulUlu,则其类型为 ulongIf the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, its type is ulong.

如果由整数字面量所表示的值超出了 UInt64.MaxValue,则将出现编译器错误 CS1021If the value represented by an integer literal exceeds UInt64.MaxValue, a compiler error CS1021 occurs.

如果确定的整数文本的类型为 int,且文本所表示的值位于目标类型的范围内,则该值可以隐式转换为 sbytebyteshortushortuintulongIf the determined type of an integer literal is int and the value represented by the literal is within the range of the destination type, the value can be implicitly converted to sbyte, byte, short, ushort, uint, or ulong:

byte a = 17;
byte b = 300;   // CS0031: Constant value '300' cannot be converted to a 'byte'

如前面的示例所示,如果文本的值不在目标类型的范围内,则发生编译器错误 CS0031As the preceding example shows, if the literal's value is not within the range of the destination type, a compiler error CS0031 occurs.

还可以使用强制转换将整数文本所表示的值转换为除确定的文本类型之外的类型:You also can use a cast to convert the value represented by an integer literal to the type other than the determined type of the literal:

var signedByte = (sbyte)42;
var longVariable = (long)42;

转换Conversions

可以将任何整型数值类型转换为其他整数数值类型。You can convert any integral numeric type to any other integral numeric type. 如果目标类型可以存储源类型的所有值,则转换是隐式的。If the destination type can store all values of the source type, the conversion is implicit. 否则,必须使用强制转换运算符 () 来调用显式转换。Otherwise, you need to use the cast operator () to invoke an explicit conversion. 有关详细信息,请参阅内置数值转换For more information, see Built-in numeric conversions.

C# 语言规范C# language specification

有关更多信息,请参阅 C# 语言规范的以下部分:For more information, see the following sections of the C# language specification:

另请参阅See also