Int64 结构

定义

表示 64 位有符号整数。

public value class long : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public value class long : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, IUtf8SpanParsable<long>, System::Numerics::IAdditionOperators<long, long, long>, System::Numerics::IAdditiveIdentity<long, long>, System::Numerics::IBinaryInteger<long>, System::Numerics::IBinaryNumber<long>, System::Numerics::IBitwiseOperators<long, long, long>, System::Numerics::IComparisonOperators<long, long, bool>, System::Numerics::IDecrementOperators<long>, System::Numerics::IDivisionOperators<long, long, long>, System::Numerics::IEqualityOperators<long, long, bool>, System::Numerics::IIncrementOperators<long>, System::Numerics::IMinMaxValue<long>, System::Numerics::IModulusOperators<long, long, long>, System::Numerics::IMultiplicativeIdentity<long, long>, System::Numerics::IMultiplyOperators<long, long, long>, System::Numerics::INumber<long>, System::Numerics::INumberBase<long>, System::Numerics::IShiftOperators<long, int, long>, System::Numerics::ISignedNumber<long>, System::Numerics::ISubtractionOperators<long, long, long>, System::Numerics::IUnaryNegationOperators<long, long>, System::Numerics::IUnaryPlusOperators<long, long>
public value class long : IComparable, IComparable<long>, IConvertible, IEquatable<long>, ISpanFormattable
public value class long : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, System::Numerics::IAdditionOperators<long, long, long>, System::Numerics::IAdditiveIdentity<long, long>, System::Numerics::IBinaryInteger<long>, System::Numerics::IBinaryNumber<long>, System::Numerics::IBitwiseOperators<long, long, long>, System::Numerics::IComparisonOperators<long, long, bool>, System::Numerics::IDecrementOperators<long>, System::Numerics::IDivisionOperators<long, long, long>, System::Numerics::IEqualityOperators<long, long, bool>, System::Numerics::IIncrementOperators<long>, System::Numerics::IMinMaxValue<long>, System::Numerics::IModulusOperators<long, long, long>, System::Numerics::IMultiplicativeIdentity<long, long>, System::Numerics::IMultiplyOperators<long, long, long>, System::Numerics::INumber<long>, System::Numerics::INumberBase<long>, System::Numerics::IShiftOperators<long, int, long>, System::Numerics::ISignedNumber<long>, System::Numerics::ISubtractionOperators<long, long, long>, System::Numerics::IUnaryNegationOperators<long, long>, System::Numerics::IUnaryPlusOperators<long, long>
public value class long : IComparable, IConvertible, IFormattable
public value class long : IComparable, IComparable<long>, IEquatable<long>, IFormattable
public struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public readonly struct Int64 : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, IUtf8SpanParsable<long>, System.Numerics.IAdditionOperators<long,long,long>, System.Numerics.IAdditiveIdentity<long,long>, System.Numerics.IBinaryInteger<long>, System.Numerics.IBinaryNumber<long>, System.Numerics.IBitwiseOperators<long,long,long>, System.Numerics.IComparisonOperators<long,long,bool>, System.Numerics.IDecrementOperators<long>, System.Numerics.IDivisionOperators<long,long,long>, System.Numerics.IEqualityOperators<long,long,bool>, System.Numerics.IIncrementOperators<long>, System.Numerics.IMinMaxValue<long>, System.Numerics.IModulusOperators<long,long,long>, System.Numerics.IMultiplicativeIdentity<long,long>, System.Numerics.IMultiplyOperators<long,long,long>, System.Numerics.INumber<long>, System.Numerics.INumberBase<long>, System.Numerics.IShiftOperators<long,int,long>, System.Numerics.ISignedNumber<long>, System.Numerics.ISubtractionOperators<long,long,long>, System.Numerics.IUnaryNegationOperators<long,long>, System.Numerics.IUnaryPlusOperators<long,long>
public readonly struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public readonly struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, ISpanFormattable
public readonly struct Int64 : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, System.Numerics.IAdditionOperators<long,long,long>, System.Numerics.IAdditiveIdentity<long,long>, System.Numerics.IBinaryInteger<long>, System.Numerics.IBinaryNumber<long>, System.Numerics.IBitwiseOperators<long,long,long>, System.Numerics.IComparisonOperators<long,long,bool>, System.Numerics.IDecrementOperators<long>, System.Numerics.IDivisionOperators<long,long,long>, System.Numerics.IEqualityOperators<long,long,bool>, System.Numerics.IIncrementOperators<long>, System.Numerics.IMinMaxValue<long>, System.Numerics.IModulusOperators<long,long,long>, System.Numerics.IMultiplicativeIdentity<long,long>, System.Numerics.IMultiplyOperators<long,long,long>, System.Numerics.INumber<long>, System.Numerics.INumberBase<long>, System.Numerics.IShiftOperators<long,int,long>, System.Numerics.ISignedNumber<long>, System.Numerics.ISubtractionOperators<long,long,long>, System.Numerics.IUnaryNegationOperators<long,long>, System.Numerics.IUnaryPlusOperators<long,long>
[System.Serializable]
public struct Int64 : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public struct Int64 : IComparable, IComparable<long>, IEquatable<long>, IFormattable
type int64 = struct
    interface IConvertible
    interface IFormattable
type int64 = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<int64>
    interface ISpanFormattable
    interface ISpanParsable<int64>
    interface IUtf8SpanFormattable
    interface IUtf8SpanParsable<int64>
    interface IAdditionOperators<int64, int64, int64>
    interface IAdditiveIdentity<int64, int64>
    interface IBinaryInteger<int64>
    interface IBinaryNumber<int64>
    interface IBitwiseOperators<int64, int64, int64>
    interface IComparisonOperators<int64, int64, bool>
    interface IEqualityOperators<int64, int64, bool>
    interface IDecrementOperators<int64>
    interface IDivisionOperators<int64, int64, int64>
    interface IIncrementOperators<int64>
    interface IModulusOperators<int64, int64, int64>
    interface IMultiplicativeIdentity<int64, int64>
    interface IMultiplyOperators<int64, int64, int64>
    interface INumber<int64>
    interface INumberBase<int64>
    interface ISubtractionOperators<int64, int64, int64>
    interface IUnaryNegationOperators<int64, int64>
    interface IUnaryPlusOperators<int64, int64>
    interface IShiftOperators<int64, int, int64>
    interface IMinMaxValue<int64>
    interface ISignedNumber<int64>
type int64 = struct
    interface IConvertible
    interface ISpanFormattable
    interface IFormattable
type int64 = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<int64>
    interface ISpanFormattable
    interface ISpanParsable<int64>
    interface IAdditionOperators<int64, int64, int64>
    interface IAdditiveIdentity<int64, int64>
    interface IBinaryInteger<int64>
    interface IBinaryNumber<int64>
    interface IBitwiseOperators<int64, int64, int64>
    interface IComparisonOperators<int64, int64, bool>
    interface IEqualityOperators<int64, int64, bool>
    interface IDecrementOperators<int64>
    interface IDivisionOperators<int64, int64, int64>
    interface IIncrementOperators<int64>
    interface IModulusOperators<int64, int64, int64>
    interface IMultiplicativeIdentity<int64, int64>
    interface IMultiplyOperators<int64, int64, int64>
    interface INumber<int64>
    interface INumberBase<int64>
    interface ISubtractionOperators<int64, int64, int64>
    interface IUnaryNegationOperators<int64, int64>
    interface IUnaryPlusOperators<int64, int64>
    interface IShiftOperators<int64, int, int64>
    interface IMinMaxValue<int64>
    interface ISignedNumber<int64>
type int64 = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<int64>
    interface ISpanFormattable
    interface ISpanParsable<int64>
    interface IAdditionOperators<int64, int64, int64>
    interface IAdditiveIdentity<int64, int64>
    interface IBinaryInteger<int64>
    interface IBinaryNumber<int64>
    interface IBitwiseOperators<int64, int64, int64>
    interface IComparisonOperators<int64, int64, bool>
    interface IEqualityOperators<int64, int64, bool>
    interface IDecrementOperators<int64>
    interface IDivisionOperators<int64, int64, int64>
    interface IIncrementOperators<int64>
    interface IModulusOperators<int64, int64, int64>
    interface IMultiplicativeIdentity<int64, int64>
    interface IMultiplyOperators<int64, int64, int64>
    interface INumber<int64>
    interface INumberBase<int64>
    interface ISubtractionOperators<int64, int64, int64>
    interface IUnaryNegationOperators<int64, int64>
    interface IUnaryPlusOperators<int64, int64>
    interface IUtf8SpanFormattable
    interface IUtf8SpanParsable<int64>
    interface IShiftOperators<int64, int, int64>
    interface IMinMaxValue<int64>
    interface ISignedNumber<int64>
[<System.Serializable>]
type int64 = struct
    interface IFormattable
    interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type int64 = struct
    interface IFormattable
    interface IConvertible
type int64 = struct
    interface IFormattable
Public Structure Int64
Implements IComparable, IComparable(Of Long), IConvertible, IEquatable(Of Long), IFormattable
Public Structure Int64
Implements IAdditionOperators(Of Long, Long, Long), IAdditiveIdentity(Of Long, Long), IBinaryInteger(Of Long), IBinaryNumber(Of Long), IBitwiseOperators(Of Long, Long, Long), IComparable(Of Long), IComparisonOperators(Of Long, Long, Boolean), IConvertible, IDecrementOperators(Of Long), IDivisionOperators(Of Long, Long, Long), IEqualityOperators(Of Long, Long, Boolean), IEquatable(Of Long), IIncrementOperators(Of Long), IMinMaxValue(Of Long), IModulusOperators(Of Long, Long, Long), IMultiplicativeIdentity(Of Long, Long), IMultiplyOperators(Of Long, Long, Long), INumber(Of Long), INumberBase(Of Long), IParsable(Of Long), IShiftOperators(Of Long, Integer, Long), ISignedNumber(Of Long), ISpanParsable(Of Long), ISubtractionOperators(Of Long, Long, Long), IUnaryNegationOperators(Of Long, Long), IUnaryPlusOperators(Of Long, Long), IUtf8SpanParsable(Of Long)
Public Structure Int64
Implements IComparable, IComparable(Of Long), IConvertible, IEquatable(Of Long), ISpanFormattable
Public Structure Int64
Implements IAdditionOperators(Of Long, Long, Long), IAdditiveIdentity(Of Long, Long), IBinaryInteger(Of Long), IBinaryNumber(Of Long), IBitwiseOperators(Of Long, Long, Long), IComparable(Of Long), IComparisonOperators(Of Long, Long, Boolean), IConvertible, IDecrementOperators(Of Long), IDivisionOperators(Of Long, Long, Long), IEqualityOperators(Of Long, Long, Boolean), IEquatable(Of Long), IIncrementOperators(Of Long), IMinMaxValue(Of Long), IModulusOperators(Of Long, Long, Long), IMultiplicativeIdentity(Of Long, Long), IMultiplyOperators(Of Long, Long, Long), INumber(Of Long), INumberBase(Of Long), IParsable(Of Long), IShiftOperators(Of Long, Integer, Long), ISignedNumber(Of Long), ISpanParsable(Of Long), ISubtractionOperators(Of Long, Long, Long), IUnaryNegationOperators(Of Long, Long), IUnaryPlusOperators(Of Long, Long)
Public Structure Int64
Implements IComparable, IConvertible, IFormattable
Public Structure Int64
Implements IComparable, IComparable(Of Long), IEquatable(Of Long), IFormattable
继承
属性
实现
IComparable IComparable<Int64> IConvertible IEquatable<Int64> IFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Int64> IParsable<TSelf> ISpanFormattable ISpanParsable<Int64> ISpanParsable<TSelf> IUtf8SpanFormattable IUtf8SpanParsable<Int64> IUtf8SpanParsable<TSelf> IAdditionOperators<Int64,Int64,Int64> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Int64,Int64> IAdditiveIdentity<TSelf,TSelf> IBinaryInteger<Int64> IBinaryNumber<Int64> IBinaryNumber<TSelf> IBitwiseOperators<Int64,Int64,Int64> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Int64,Int64,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Int64> IDecrementOperators<TSelf> IDivisionOperators<Int64,Int64,Int64> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Int64,Int64,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IIncrementOperators<Int64> IIncrementOperators<TSelf> IMinMaxValue<Int64> IModulusOperators<Int64,Int64,Int64> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Int64,Int64> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Int64,Int64,Int64> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Int64> INumber<TSelf> INumberBase<Int64> INumberBase<TSelf> IShiftOperators<Int64,Int32,Int64> IShiftOperators<TSelf,Int32,TSelf> ISignedNumber<Int64> ISubtractionOperators<Int64,Int64,Int64> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<Int64,Int64> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Int64,Int64> IUnaryPlusOperators<TSelf,TSelf>

注解

Int64 是一个不可变值类型,表示有符号整数,其值范围为负 9,223,372,036,854,775,808(由 Int64.MinValue 常量表示)到正 9,223,372,036,854,775,807(由 Int64.MaxValue 常量表示)。 .NET 还包含一个无符号 64 位整数值类型, UInt64表示范围为 0 到 18,446,744,073,709,551,615 的值。

实例化 Int64 值

可以通过多种方式实例化 Int64 值:

  • 可以声明变量 Int64 ,并为其分配数据类型范围内的 Int64 文本整数值。 以下示例声明两个 Int64 变量,并以此方式为其分配值。

    long number1 = -64301728;
    long number2 = 255486129307;
    
    let number1 = -64301728L
    let number2 = 255486129307L
    
    Dim number1 As Long = -64301728
    Dim number2 As Long = 255486129307
    
  • 可以把一个其范围是 Int64 类型子集的整型类型的值赋给变量。 这是一种扩宽转换,在 C# 中不需要强制转换运算符,在 Visual Basic 中也不需要转换方法。 在 F# 中,只有类型 Int32 可以自动扩展。

    sbyte value1 = 124;
    short value2 = 1618;
    int value3 = Int32.MaxValue;
    
    long number1 = value1;
    long number2 = value2;
    long number3 = value3;
    
    let value1 = 124y
    let value2 = 1618s
    let value3 = Int32.MaxValue
    
    let number1 = int64 value1
    let number2 = int64 value2
    let number3: int64 = value3
    
    Dim value1 As SByte = 124
    Dim value2 As Int16 = 1618
    Dim value3 As Int32 = Int32.MaxValue
    
    Dim number1 As Long = value1
    Dim number2 As Long = value2
    Dim number3 As Long = value3
    
  • 可以分配其范围超过该 Int64 类型的数值类型的值。 这是一个缩小的转换,因此在 C# 或 F# 中需要使用类型转换运算符,而在 Visual Basic 中需要使用转换方法,前提是 Option Strict 已启用。 如果数值是包含SingleDouble小数部分的值,Decimal则其小数部分的处理取决于执行转换的编译器。 以下示例执行缩小转换,将多个数值 Int64 分配给变量。

    ulong ulNumber = 163245617943825;
    try {
       long number1 = (long) ulNumber;
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine($"{ulNumber} is out of range of an Int64.");
    }
    
    double dbl2 = 35901.997;
    try {
       long number2 = (long) dbl2;
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine($"{dbl2} is out of range of an Int64.");
    }
    
    BigInteger bigNumber = (BigInteger) 1.63201978555e30;
    try {
       long number3 = (long) bigNumber;
       Console.WriteLine(number3);
    }
    catch (OverflowException) {
       Console.WriteLine($"{bigNumber} is out of range of an Int64.");
    }
    // The example displays the following output:
    //    163245617943825
    //    35902
    //    1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
    
    let ulNumber = 163245617943825uL
    try
        let number1 = int64 ulNumber
        printfn $"{number1}"
    with :? OverflowException ->
        printfn $"{ulNumber} is out of range of an Int64."
    
    let dbl2 = 35901.997
    try
        let number2 = int64 dbl2
        printfn $"{number2}"
    with :? OverflowException ->
        printfn $"{dbl2} is out of range of an Int64."
    
    let bigNumber = BigInteger 1.63201978555e30
    try
        let number3 = int64 bigNumber
        printfn $"{number3}"
    with :? OverflowException ->
        printfn $"{bigNumber} is out of range of an Int64."
    
    // The example displays the following output:
    //    163245617943825
    //    35902
    //    1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
    
    Dim ulNumber As ULong = 163245617943825
    Try
       Dim number1 As Long = CLng(ulNumber)
       Console.WriteLine(number1)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int64.", ulNumber)
    End Try
    
    Dim dbl2 As Double = 35901.997
    Try
       Dim number2 As Long = CLng(dbl2)
       Console.WriteLine(number2)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int64.", dbl2)
    End Try
       
    Dim bigNumber As BigInteger = 1.63201978555e30
    Try
       Dim number3 As Long = CLng(bigNumber)
       Console.WriteLine(number3)
    Catch e As OverflowException
       Console.WriteLine("{0:N0} is out of range of an Int64.", bigNumber)
    End Try    
    ' The example displays the following output:
    '    163245617943825
    '    35902
    '    1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
    
  • 可以调用类的方法 Convert ,将任何受支持的类型转换为值 Int64 。 这是可能的,因为 Int64 支持 IConvertible 接口。 下面的示例演示了值数组 DecimalInt64 值的转换。

    decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m,
                        199.55m, 9214.16m, Decimal.MaxValue };
    long result;
    
    foreach (decimal value in values)
    {
       try {
          result = Convert.ToInt64(value);
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                            value.GetType().Name, value,
                            result.GetType().Name, result);
       }
       catch (OverflowException) {
          Console.WriteLine("{0} is outside the range of the Int64 type.",
                            value);
       }
    }
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int64 type.
    //    Converted the Decimal value '-1034.23' to the Int64 value -1034.
    //    Converted the Decimal value '-12' to the Int64 value -12.
    //    Converted the Decimal value '0' to the Int64 value 0.
    //    Converted the Decimal value '147' to the Int64 value 147.
    //    Converted the Decimal value '199.55' to the Int64 value 200.
    //    Converted the Decimal value '9214.16' to the Int64 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int64 type.
    
    let values = 
        [| Decimal.MinValue; -1034.23m; -12m; 0m; 147m
           199.55m; 9214.16m; Decimal.MaxValue |]
    
    for value in values do
        try
            let result = Convert.ToInt64 value
            printfn $"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."
        with :? OverflowException ->
            printfn $"{value} is outside the range of the Int64 type."
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int64 type.
    //    Converted the Decimal value '-1034.23' to the Int64 value -1034.
    //    Converted the Decimal value '-12' to the Int64 value -12.
    //    Converted the Decimal value '0' to the Int64 value 0.
    //    Converted the Decimal value '147' to the Int64 value 147.
    //    Converted the Decimal value '199.55' to the Int64 value 200.
    //    Converted the Decimal value '9214.16' to the Int64 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int64 type.
    
    Dim values() As Decimal = { Decimal.MinValue, -1034.23d, -12d, 0d, 147d, _
                                199.55d, 9214.16d, Decimal.MaxValue }
    Dim result As Long
    
    For Each value As Decimal In values
       Try
          result = Convert.ToInt64(value)
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                            value.GetType().Name, value, _
                            result.GetType().Name, result)
       Catch e As OverflowException
          Console.WriteLine("{0} is outside the range of the Int64 type.", _
                            value)
       End Try
    Next
    ' The example displays the following output:
    '    -79228162514264337593543950335 is outside the range of the Int64 type.
    '    Converted the Decimal value '-1034.23' to the Int64 value -1034.
    '    Converted the Decimal value '-12' to the Int64 value -12.
    '    Converted the Decimal value '0' to the Int64 value 0.
    '    Converted the Decimal value '147' to the Int64 value 147.
    '    Converted the Decimal value '199.55' to the Int64 value 200.
    '    Converted the Decimal value '9214.16' to the Int64 value 9214.
    '    79228162514264337593543950335 is outside the range of the Int64 type.
    
  • 可以调用 ParseTryParse 方法将值的字符串表示形式 Int64 转换为 Int64。 该字符串可以包含十进制或十六进制数字。 以下示例使用十进制字符串和十六进制字符串演示了分析作。

    string string1 = "244681903147";
    try {
       long number1 = Int64.Parse(string1);
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine($"'{string1}' is out of range of a 64-bit integer.");
    }
    catch (FormatException) {
       Console.WriteLine($"The format of '{string1}' is invalid.");
    }
    
    string string2 = "F9A3CFF0A";
    try {
       long number2 = Int64.Parse(string2,
                                  System.Globalization.NumberStyles.HexNumber);
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine($"'{string2}' is out of range of a 64-bit integer.");
    }
    catch (FormatException) {
       Console.WriteLine($"The format of '{string2}' is invalid.");
    }
    // The example displays the following output:
    //    244681903147
    //    67012198154
    
    let string1 = "244681903147"
    try
        let number1 = Int64.Parse string1
        printfn $"{number1}"
    with
    | :? OverflowException ->
        printfn $"'{string1}' is out of range of a 64-bit integer."
    | :? FormatException ->
        printfn $"The format of '{string1}' is invalid."
    
    let string2 = "F9A3CFF0A"
    try
        let number2 = Int64.Parse(string2, NumberStyles.HexNumber)
        printfn $"{number2}"
    
    with
    | :? OverflowException ->
        printfn $"'{string2}' is out of range of a 64-bit integer."
    | :? FormatException ->
        printfn $"The format of '{string2}' is invalid."
    
    // The example displays the following output:
    //    244681903147
    //    67012198154
    
    Dim string1 As String = "244681903147"
    Try
       Dim number1 As Long = Int64.Parse(string1)
       Console.WriteLine(number1)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string1)
    Catch e As FormatException
       Console.WriteLine("The format of '{0}' is invalid.", string1)
    End Try
    
    Dim string2 As String = "F9A3CFF0A"
    Try
       Dim number2 As Long = Int64.Parse(string2,
                                System.Globalization.NumberStyles.HexNumber)
       Console.WriteLine(number2)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string2)
    Catch e As FormatException
       Console.WriteLine("The format of '{0}' is invalid.", string2)
    End Try
    ' The example displays the following output:
    '    244681903147
    '    67012198154
    

对 Int64 值执行操作

Int64 类型支持标准数学运算,例如加法、减法、除法、乘法、求反和一元求反。 与其他整型类型一样,该 Int64 类型还支持按位 ANDORXOR、左移和右移运算符。

可以使用标准数值运算符比较两个 Int64 值,也可以调用 CompareToEquals 方法。

还可以调用类的成员 Math 来执行各种数值运算,包括获取数字的绝对值、计算积分除法的商和余数、确定两个长整数的最大值或最小值、获取数字的符号以及舍入数字。

将 Int64 表示为字符串

Int64 类型为标准和自定义数字格式字符串提供完全支持。 (有关详细信息,请参阅 格式类型标准数字格式字符串自定义数字格式字符串

若要将 Int64 值格式化为无前导零的整型字符串,可以调用无 ToString() 参数方法。 通过使用“D”格式说明符,还可以在字符串表示形式中包含指定数量的前导零。 通过使用“N”格式说明符,可以包括组分隔符,并指定要显示在数字的字符串表示形式的十进制数字数。 通过使用“X”格式说明符,可以将值 Int64 表示为十六进制字符串。 以下示例采用以下四种方式设置值数组 Int64 中的元素的格式。

long[] numbers = { -1403, 0, 169, 1483104 };
foreach (var number in numbers)
{
    // Display value using default formatting.
    Console.Write("{0,-8}  -->   ", number.ToString());
    // Display value with 3 digits and leading zeros.
    Console.Write("{0,8:D3}", number);
    // Display value with 1 decimal digit.
    Console.Write("{0,13:N1}", number);
    // Display value as hexadecimal.
    Console.Write("{0,18:X2}", number);
    // Display value with eight hexadecimal digits.
    Console.WriteLine("{0,18:X8}", number);
}
// The example displays the following output:
//    -1403     -->      -1403     -1,403.0  FFFFFFFFFFFFFA85  FFFFFFFFFFFFFA85
//    0         -->        000          0.0                00          00000000
//    169       -->        169        169.0                A9          000000A9
//    1483104   -->    1483104  1,483,104.0            16A160          0016A160
let numbers = [| -1403L; 0L; 169L; 1483104L |]
for number in numbers do
    // Display value using default formatting.
    printf $"{number.ToString(),-8}  -->   "
    // Display value with 3 digits and leading zeros.
    printf $"{number,8:D3}"
    // Display value with 1 decimal digit.
    printf $"{number,13:N1}"
    // Display value as hexadecimal.
    printf $"{number,18:X2}"
    // Display value with eight hexadecimal digits.
    printfn $"{number,18:X8}"

// The example displays the following output:
//    -1403     -->      -1403     -1,403.0  FFFFFFFFFFFFFA85  FFFFFFFFFFFFFA85
//    0         -->        000          0.0                00          00000000
//    169       -->        169        169.0                A9          000000A9
//    1483104   -->    1483104  1,483,104.0            16A160          0016A160
Dim numbers() As Long = { -1403, 0, 169, 1483104 }
For Each number In numbers
   ' Display value using default formatting.
   Console.Write("{0,-8}  -->   ", number.ToString())
   ' Display value with 3 digits and leading zeros.
   Console.Write("{0,8:D3}", number)
   ' Display value with 1 decimal digit.
   Console.Write("{0,13:N1}", number) 
   ' Display value as hexadecimal.
   Console.Write("{0,18:X2}", number)
   ' Display value with eight hexadecimal digits.
   Console.WriteLine("{0,18:X8}", number)
Next   
' The example displays the following output:
'    -1403     -->      -1403     -1,403.0  FFFFFFFFFFFFFA85  FFFFFFFFFFFFFA85
'    0         -->        000          0.0                00          00000000
'    169       -->        169        169.0                A9          000000A9
'    1483104   -->    1483104  1,483,104.0            16A160          0016A160

还可以通过调用Int64方法并提供基作为方法的第二个参数,将值格式化ToString(Int64, Int32)为二进制、八进制、十六进制或十六进制字符串。 以下示例调用此方法以显示整数值的数组的二进制、八进制和十六进制表示形式。

long[] numbers = { -146, 11043, 2781913 };
foreach (var number in numbers)
{
    Console.WriteLine($"{number} (Base 10):");
    Console.WriteLine($"   Binary:  {Convert.ToString(number, 2)}");
    Console.WriteLine($"   Octal:   {Convert.ToString(number, 8)}");
    Console.WriteLine($"   Hex:     {Convert.ToString(number, 16)}{Environment.NewLine}");
}
// The example displays the following output:
//    -146 (Base 10):
//       Binary:  1111111111111111111111111111111111111111111111111111111101101110
//       Octal:   1777777777777777777556
//       Hex:     ffffffffffffff6e
//
//    11043 (Base 10):
//       Binary:  10101100100011
//       Octal:   25443
//       Hex:     2b23
//
//    2781913 (Base 10):
//       Binary:  1010100111001011011001
//       Octal:   12471331
//       Hex:     2a72d9
let numbers = [| -146L; 11043L; 2781913L |]
for number in numbers do
    printfn $"{number} (Base 10):"
    printfn $"   Binary:  {Convert.ToString(number, 2)}"
    printfn $"   Octal:   {Convert.ToString(number, 8)}"
    printfn $"   Hex:     {Convert.ToString(number, 16)}\n"

// The example displays the following output:
//    -146 (Base 10):
//       Binary:  1111111111111111111111111111111111111111111111111111111101101110
//       Octal:   1777777777777777777556
//       Hex:     ffffffffffffff6e
//
//    11043 (Base 10):
//       Binary:  10101100100011
//       Octal:   25443
//       Hex:     2b23
//
//    2781913 (Base 10):
//       Binary:  1010100111001011011001
//       Octal:   12471331
//       Hex:     2a72d9
Dim numbers() As Long = { -146, 11043, 2781913 }
For Each number In numbers
   Console.WriteLine("{0} (Base 10):", number)
   Console.WriteLine("   Binary:  {0}", Convert.ToString(number, 2))
   Console.WriteLine("   Octal:   {0}", Convert.ToString(number, 8))
   Console.WriteLine("   Hex:     {0}", Convert.ToString(number, 16))
   Console.WriteLine()
Next      
' The example displays the following output:
'    -146 (Base 10):
'       Binary:  1111111111111111111111111111111111111111111111111111111101101110
'       Octal:   1777777777777777777556
'       Hex:     ffffffffffffff6e
'
'    11043 (Base 10):
'       Binary:  10101100100011
'       Octal:   25443
'       Hex:     2b23
'
'    2781913 (Base 10):
'       Binary:  1010100111001011011001
'       Octal:   12471331
'       Hex:     2a72d9

使用非十进制 64 位整数值进行操作

除了将单个长整数用作十进制值之外,你可能还希望执行按位运算或处理长整数值的二进制或十六进制表示形式。 Int64 值以 63 位表示,第 64 位用作符号位。 正值使用符号和数量级表示形式表示。 负值采用二进制补码表示形式。 在对 Int64 值执行按位作或处理单个位时,请务必记住这一点。 若要对任何两个非十进制值执行数值、布尔或比较运算,这两个值都必须使用相同的表示形式。

字段

名称 说明
MaxValue

表示一个 Int64的最大可能值。 此字段为常量。

MinValue

表示一个 Int64. 的最小可能值。 此字段为常量。

方法

名称 说明
Abs(Int64)

计算值的绝对值。

BigMul(Int64, Int64)

生成两个 64 位数字的完整乘积。

Clamp(Int64, Int64, Int64)

将值固定到非独占最小值和最大值。

CompareTo(Int64)

将此实例与指定的 64 位有符号整数进行比较,并返回其相对值的指示。

CompareTo(Object)

将此实例与指定的对象进行比较,并返回其相对值的指示。

CopySign(Int64, Int64)

将值的符号复制到另一个值的符号。

CreateChecked<TOther>(TOther)

从值创建当前类型的实例,为超出当前类型的可表示范围的任何值引发溢出异常。

CreateSaturating<TOther>(TOther)

从值创建当前类型的实例,使属于当前类型的可表示范围之外的任何值饱和。

CreateTruncating<TOther>(TOther)

从值创建当前类型的实例,截断属于当前类型的可表示范围之外的任何值。

DivRem(Int64, Int64)

计算两个值的商和余数。

Equals(Int64)

返回一个值,该值指示此实例是否等于指定 Int64 值。

Equals(Object)

返回一个值,该值指示此实例是否等于指定的对象。

GetHashCode()

返回此实例的哈希代码。

GetTypeCode()

返回 TypeCode 值类型的 Int64值 。

IsEvenInteger(Int64)

确定值是否表示偶数。

IsNegative(Int64)

确定值是否为负值。

IsOddInteger(Int64)

确定值是否表示奇数整数。

IsPositive(Int64)

确定值是否为正值。

IsPow2(Int64)

确定值是否为 2 的幂。

LeadingZeroCount(Int64)

计算值中的前导零数。

Log10(Int64)

表示 64 位有符号整数。

Log2(Int64)

计算值的 log2。

Max(Int64, Int64)

比较两个值与计算值更大。

MaxMagnitude(Int64, Int64)

比较两个值与计算值更大。

Min(Int64, Int64)

比较两个值与计算值较小。

MinMagnitude(Int64, Int64)

比较两个值与计算值较小。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

将 UTF-8 字符的范围分析为值。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

将 UTF-8 字符的范围分析为值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符的范围分析为值。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的跨度表示形式转换为其等效的 64 位带符号整数。

Parse(String, IFormatProvider)

将指定区域性特定格式的数字的字符串表示形式转换为其等效的 64 位带符号整数。

Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为其等效的 64 位带符号整数。

Parse(String, NumberStyles)

将指定样式中的数字的字符串表示形式转换为其等效的 64 位带符号整数。

Parse(String)

将数字的字符串表示形式转换为其等效的 64 位带符号整数。

PopCount(Int64)

计算值中设置的位数。

RotateLeft(Int64, Int32)

按给定量旋转一个值。

RotateRight(Int64, Int32)

按给定的量向右旋转值。

Sign(Int64)

计算值的符号。

ToString()

将此实例的数值转换为其等效的字符串表示形式。

ToString(IFormatProvider)

使用指定的区域性特定格式信息将此实例的数值转换为其等效的字符串表示形式。

ToString(String, IFormatProvider)

使用指定的格式和区域性特定的格式信息将此实例的数值转换为其等效的字符串表示形式。

ToString(String)

使用指定的格式将此实例的数值转换为其等效的字符串表示形式。

TrailingZeroCount(Int64)

计算值中的尾随零数。

TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)

尝试将当前实例的值格式化为 UTF-8,并将其设置为提供的字节范围。

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

尝试将当前长数实例的值格式化为提供的字符范围。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int64)

尝试将 UTF-8 字符的范围分析为值。

TryParse(ReadOnlySpan<Byte>, Int64)

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的 64 位带符号整数。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int64)

尝试将 UTF-8 字符的范围分析为值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int64)

尝试将字符范围分析为值。

TryParse(ReadOnlySpan<Char>, Int64)

将数字的跨度表示形式转换为其等效的 64 位带符号整数。 返回值指示转换是成功还是失败。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int64)

将指定样式和区域性特定格式的数字的跨度表示形式转换为其等效的 64 位带符号整数。 返回值指示转换是成功还是失败。

TryParse(String, IFormatProvider, Int64)

尝试将字符串分析为值。

TryParse(String, Int64)

将数字的字符串表示形式转换为其等效的 64 位带符号整数。 返回值指示转换是成功还是失败。

TryParse(String, NumberStyles, IFormatProvider, Int64)

将指定样式和区域性特定格式的数字的字符串表示形式转换为其等效的 64 位带符号整数。 返回值指示转换是成功还是失败。

显式接口实现

名称 说明
IAdditionOperators<Int64,Int64,Int64>.Addition(Int64, Int64)

将两个值相加以计算其总和。

IAdditionOperators<Int64,Int64,Int64>.CheckedAddition(Int64, Int64)

将两个值相加以计算其总和。

IAdditiveIdentity<Int64,Int64>.AdditiveIdentity

获取当前类型的累加标识。

IBinaryInteger<Int64>.GetByteCount()

获取将作为其一部分 TryWriteLittleEndian(Span<Byte>, Int32)写入的字节数。

IBinaryInteger<Int64>.GetShortestBitLength()

获取最短的两个当前值的补数表示形式的长度(以位为单位)。

IBinaryInteger<Int64>.TryReadBigEndian(ReadOnlySpan<Byte>, Boolean, Int64)

表示 64 位有符号整数。

IBinaryInteger<Int64>.TryReadLittleEndian(ReadOnlySpan<Byte>, Boolean, Int64)

表示 64 位有符号整数。

IBinaryInteger<Int64>.TryWriteBigEndian(Span<Byte>, Int32)

尝试将当前值(采用 big-endian 格式)写入给定范围。

IBinaryInteger<Int64>.TryWriteLittleEndian(Span<Byte>, Int32)

尝试将当前值(以小端格式)写入给定范围。

IBinaryNumber<Int64>.AllBitsSet

获取在其中设置所有位的二进制类型的实例。

IBitwiseOperators<Int64,Int64,Int64>.BitwiseAnd(Int64, Int64)

计算两个值的按位和两个值。

IBitwiseOperators<Int64,Int64,Int64>.BitwiseOr(Int64, Int64)

计算两个值的按位或两个值。

IBitwiseOperators<Int64,Int64,Int64>.ExclusiveOr(Int64, Int64)

计算独占值或两个值。

IBitwiseOperators<Int64,Int64,Int64>.OnesComplement(Int64)

计算给定值的补补表示形式。

IComparable.CompareTo(Object)

将此实例与指定的对象进行比较,并返回其相对值的指示。

IComparisonOperators<Int64,Int64,Boolean>.GreaterThan(Int64, Int64)

比较两个值以确定哪个值更大。

IComparisonOperators<Int64,Int64,Boolean>.GreaterThanOrEqual(Int64, Int64)

比较两个值以确定哪个值更大或相等。

IComparisonOperators<Int64,Int64,Boolean>.LessThan(Int64, Int64)

比较两个值以确定哪个值更少。

IComparisonOperators<Int64,Int64,Boolean>.LessThanOrEqual(Int64, Int64)

比较两个值以确定哪个值小于或等于。

IConvertible.GetTypeCode()

返回 TypeCode 值类型的 Int64值 。

IConvertible.ToBoolean(IFormatProvider)

有关此成员的说明,请参阅 ToBoolean(IFormatProvider)

IConvertible.ToByte(IFormatProvider)

有关此成员的说明,请参阅 ToByte(IFormatProvider)

IConvertible.ToChar(IFormatProvider)

有关此成员的说明,请参阅 ToChar(IFormatProvider)

IConvertible.ToDateTime(IFormatProvider)

不支持此转换。 尝试使用此方法会引发一个 InvalidCastException

IConvertible.ToDecimal(IFormatProvider)

有关此成员的说明,请参阅 ToDecimal(IFormatProvider)

IConvertible.ToDouble(IFormatProvider)

有关此成员的说明,请参阅 ToDouble(IFormatProvider)

IConvertible.ToInt16(IFormatProvider)

有关此成员的说明,请参阅 ToInt16(IFormatProvider)

IConvertible.ToInt32(IFormatProvider)

有关此成员的说明,请参阅 ToInt32(IFormatProvider)

IConvertible.ToInt64(IFormatProvider)

有关此成员的说明,请参阅 ToInt64(IFormatProvider)

IConvertible.ToSByte(IFormatProvider)

有关此成员的说明,请参阅 ToSByte(IFormatProvider)

IConvertible.ToSingle(IFormatProvider)

有关此成员的说明,请参阅 ToSingle(IFormatProvider)

IConvertible.ToType(Type, IFormatProvider)

有关此成员的说明,请参阅 ToType(Type, IFormatProvider)

IConvertible.ToUInt16(IFormatProvider)

有关此成员的说明,请参阅 ToUInt16(IFormatProvider)

IConvertible.ToUInt32(IFormatProvider)

有关此成员的说明,请参阅 ToUInt32(IFormatProvider)

IConvertible.ToUInt64(IFormatProvider)

有关此成员的说明,请参阅 ToUInt64(IFormatProvider)

IDecrementOperators<Int64>.CheckedDecrement(Int64)

递减值。

IDecrementOperators<Int64>.Decrement(Int64)

递减值。

IDivisionOperators<Int64,Int64,Int64>.Division(Int64, Int64)

将一个值除以计算其商。

IEqualityOperators<Int64,Int64,Boolean>.Equality(Int64, Int64)

比较两个值以确定相等性。

IEqualityOperators<Int64,Int64,Boolean>.Inequality(Int64, Int64)

比较两个值以确定不相等性。

IIncrementOperators<Int64>.CheckedIncrement(Int64)

递增值。

IIncrementOperators<Int64>.Increment(Int64)

递增值。

IMinMaxValue<Int64>.MaxValue

获取当前类型的最大值。

IMinMaxValue<Int64>.MinValue

获取当前类型的最小值。

IModulusOperators<Int64,Int64,Int64>.Modulus(Int64, Int64)

将两个值相除以计算其模数或余数。

IMultiplicativeIdentity<Int64,Int64>.MultiplicativeIdentity

获取当前类型的乘法标识。

IMultiplyOperators<Int64,Int64,Int64>.CheckedMultiply(Int64, Int64)

将两个值相乘以计算其产品。

IMultiplyOperators<Int64,Int64,Int64>.Multiply(Int64, Int64)

将两个值相乘以计算其产品。

INumber<Int64>.MaxNumber(Int64, Int64)

将两个值与计算进行比较,如果输入 NaN是,则返回另一个值。

INumber<Int64>.MinNumber(Int64, Int64)

将两个值与计算进行比较,如果输入 NaN较小,则返回另一个值。

INumberBase<Int64>.IsCanonical(Int64)

确定某个值是否在其规范表示形式中。

INumberBase<Int64>.IsComplexNumber(Int64)

确定值是否表示复数。

INumberBase<Int64>.IsFinite(Int64)

确定值是否有限。

INumberBase<Int64>.IsImaginaryNumber(Int64)

确定值是否表示纯虚数。

INumberBase<Int64>.IsInfinity(Int64)

确定值是否无限。

INumberBase<Int64>.IsInteger(Int64)

确定值是否表示整数。

INumberBase<Int64>.IsNaN(Int64)

确定值是否为 NaN。

INumberBase<Int64>.IsNegativeInfinity(Int64)

确定值是否为负无穷大。

INumberBase<Int64>.IsNormal(Int64)

确定值是否正常。

INumberBase<Int64>.IsPositiveInfinity(Int64)

确定值是否为正无穷大。

INumberBase<Int64>.IsRealNumber(Int64)

确定值是否表示实数。

INumberBase<Int64>.IsSubnormal(Int64)

确定某个值是否为非正常值。

INumberBase<Int64>.IsZero(Int64)

确定值是否为零。

INumberBase<Int64>.MaxMagnitudeNumber(Int64, Int64)

将两个值与计算进行比较,如果输入 NaN是,则返回另一个值。

INumberBase<Int64>.MinMagnitudeNumber(Int64, Int64)

将两个值与计算进行比较,如果输入 NaN是,则返回另一个值。

INumberBase<Int64>.MultiplyAddEstimate(Int64, Int64, Int64)

计算估计值 (left * right) + 。 addend

INumberBase<Int64>.One

获取类型的值 1

INumberBase<Int64>.Radix

获取类型的基数。

INumberBase<Int64>.TryConvertFromChecked<TOther>(TOther, Int64)

表示 64 位有符号整数。

INumberBase<Int64>.TryConvertFromSaturating<TOther>(TOther, Int64)

表示 64 位有符号整数。

INumberBase<Int64>.TryConvertFromTruncating<TOther>(TOther, Int64)

表示 64 位有符号整数。

INumberBase<Int64>.TryConvertToChecked<TOther>(Int64, TOther)

尝试将当前类型的实例转换为另一种类型,为超出当前类型可表示范围的任何值引发溢出异常。

INumberBase<Int64>.TryConvertToSaturating<TOther>(Int64, TOther)

尝试将当前类型的实例转换为另一种类型,使超出当前类型的可表示范围的任何值饱和。

INumberBase<Int64>.TryConvertToTruncating<TOther>(Int64, TOther)

尝试将当前类型的实例转换为另一种类型,截断属于当前类型的可表示范围之外的任何值。

INumberBase<Int64>.Zero

获取类型的值 0

IShiftOperators<Int64,Int32,Int64>.LeftShift(Int64, Int32)

将一个值左移一个给定的量。

IShiftOperators<Int64,Int32,Int64>.RightShift(Int64, Int32)

将值右移一个给定的量。

IShiftOperators<Int64,Int32,Int64>.UnsignedRightShift(Int64, Int32)

将值右移一个给定的量。

ISignedNumber<Int64>.NegativeOne

获取类型的值 -1

ISubtractionOperators<Int64,Int64,Int64>.CheckedSubtraction(Int64, Int64)

减去两个值以计算其差异。

ISubtractionOperators<Int64,Int64,Int64>.Subtraction(Int64, Int64)

减去两个值以计算其差异。

IUnaryNegationOperators<Int64,Int64>.CheckedUnaryNegation(Int64)

计算值选中的一元求反。

IUnaryNegationOperators<Int64,Int64>.UnaryNegation(Int64)

计算值的一元求反。

IUnaryPlusOperators<Int64,Int64>.UnaryPlus(Int64)

计算一元加值。

适用于

线程安全性

此类型的所有成员都是线程安全的。 似乎修改实例状态的成员实际上返回使用新值初始化的新实例。 与任何其他类型一样,读取和写入包含此类型的实例的共享变量必须受到锁的保护,以确保线程安全。

另请参阅