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

“整型数值类型”是“简单类型”的子集,可以使用文本进行初始化The integral numeric types are a subset of the simple types and can be initialized with literals. 所有整型类型同时也是值类型。All integral types are also value types. 所有整型数值类型都支持算术位逻辑比较和相等运算符。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.

整型文本Integral literals

整型文本可以指定为十进制文本、十六进制文本或二进制文本 。Integral literals can be specified as decimal literals, hexadecimal literals, or binary literals. 每种文本的示例如下所示:An example of each is shown below:

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

十进制文本不需要任何前缀。Decimal literals don't require any prefix. xX 前缀表示十六进制文本 。The x or X prefix signifies a hexadecimal literal. bB 前缀表示二进制文本 。The b or B prefix signifies a binary literal. binaryLiteral 的声明演示将 _ 用作数字分隔符 。The declaration of binaryLiteral demonstrates the use of _ as a digit separator. 数字分隔符可以与所有数值文本一起使用。The digit separator can be used with all numeric literals. C# 7.0 及以后版本均支持二进制文本和数字分隔符 _Binary literals and the digit separator _ are supported starting with C# 7.0.

文本后缀Literal suffixes

lL 后缀指定整型文本应为 long 类型。The l or L suffix specifies that the integral literal should be of the long type. ulUL 后缀指定 ulong 类型。The ul or UL suffix specifies the ulong type. 如果对大于 9,223,372,036,854,775,807(long 的最大值)的文本使用 L 后缀,则该值将转换为 ulong 类型。If the L suffix is used on a literal that is greater than 9,223,372,036,854,775,807 (the maximum value of long), the value is converted to the ulong type. 如果整型文本表示的值超出了 UInt64.MaxValue,就会出现编译器错误 CS1021If the value represented by an integral literal exceeds UInt64.MaxValue, a compiler error CS1021 occurs.

备注

也可用小写字母“l”作后缀。You can use the lowercase letter "l" as a suffix. 但是,字母“l”容易与数字“1”混淆,因此会生成编译器警告。However, this generates a compiler warning because the letter "l" is easily confused with the digit "1." 为清楚起见,请使用“L”。Use "L" for clarity.

整型文本类型Type of an integral literal

如果整型文本没有后缀,则其类型为以下类型中可表示其值的第一个类型:If an integral 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

可以通过赋值或强制转换,将整型文本转换为范围小于默认值的类型:You can convert an integral literal to a type with a smaller range than the default using either an assignment or a cast:

byte byteVariable = 42; // type is byte
var signedByte = (sbyte)42; // type is sbyte.

可以通过赋值、强制转换或文本后缀将整型文本转换为范围大于默认值的类型:You can convert an integral literal to a type with a larger range than the default using either assignment, a cast, or a suffix on the literal:

var unsignedLong = 42UL;
var longVariable = 42L;
ulong anotherUnsignedLong = 42;
var anotherLong = (long)42;

转换Conversions

任何两个整型类型之间均可进行隐式转换(称为扩大转换),在转换中,目标类型可以存储源类型的所有值 。There's an implicit conversion (called a widening conversion) between any two integral types where the destination type can store all values of the source type. 例如,int 可隐式转换为 long,因为 int 值的范围是 long 的真子集。For example, there's an implicit conversion from int to long because the range of int values is a proper subset of long. 小型无符号整型类型可隐式转换为大型带符号整型类型。There are implicit conversions from a smaller unsigned integral type to a larger signed integral type. 任何整型类型都可隐式转换为任何浮点类型。There's also an implicit conversion from any integral type to any floating-point type. 任何带符号整型类型无法隐式转换为任何无符号整型类型。There's no implicit conversion from any signed integral type to any unsigned integral type.

如果未定义源类型到目标类型的隐式转换,必须使用显式强制转换将整型类型转换为其他整型类型。You must use an explicit cast to convert one integral type to another integral type when an implicit conversion is not defined from the source type to the destination type. 这称为收缩转换 。This is called a narrowing conversion. 由于转换可能导致数据丢失,因此必须使用显式用例。The explicit case is required because the conversion can result in data loss.

请参阅See also