Int32 結構

定義

表示32位帶正負號的整數。

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

備註

Int32 是一種不可變的值類型,表示帶符號的整數,其值範圍從負 2,147,483,648(由 Int32.MinValue 常數表示)到正 2,147,483,647(由 Int32.MaxValue 常數表示)。 .NET 也包含不帶正負號的 32 位整數實值型別, UInt32代表範圍從 0 到 4,294,967,295 的值。

具現化 Int32 值

您可以透過數種方式具現化 Int32 值:

  • 您可以宣告 Int32 變數,並將其指派 Int32 數據類型範圍內的常值整數值。 下列範例會宣告兩個 Int32 變數,並以這種方式指派值。

    int number1 = 64301;
    int number2 = 25548612;
    
    let number1 = 64301
    let number2 = 25548612
    
    Dim number1 As Integer = 64301
    Dim number2 As Integer = 25548612
    
  • 您可以指派整數型別的值,其範圍是型別 Int32 的子集。 這是擴大轉換,不需要 C# 中的轉換運算元或 Visual Basic 中的轉換方法,但在 F# 中則需要轉換運算元。

    sbyte value1 = 124;
    short value2 = 1618;
    
    int number1 = value1;
    int number2 = value2;
    
    let value1 = 124y
    let value2 = 1618s
    
    let number1 = int value1
    let number2 = int value2
    
    Dim value1 As SByte = 124
    Dim value2 As Int16 = 1618
    
    Dim number1 As Integer = value1
    Dim number2 As Integer = value2
    
  • 您可以指派數值類型的值,其範圍超過 Int32 類型的值。 這是縮小型別轉換,因此它需要在 C# 或 F# 中使用轉型運算子,如果 "Option Strict" 開啟,則需要在 Visual Basic 中使用轉換方法。 如果數值是 SingleDouble或包含小數位元件的 Decimal 值,其小數部分的處理取決於執行轉換的編譯程式。 下列範例會執行縮小轉換,將數個數值指派給 Int32 變數。

    long lNumber = 163245617;
    try {
       int number1 = (int) lNumber;
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine($"{lNumber} is out of range of an Int32.");
    }
    
    double dbl2 = 35901.997;
    try {
       int number2 = (int) dbl2;
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine($"{dbl2} is out of range of an Int32.");
    }
    
    BigInteger bigNumber = 132451;
    try {
       int number3 = (int) bigNumber;
       Console.WriteLine(number3);
    }
    catch (OverflowException) {
       Console.WriteLine($"{bigNumber} is out of range of an Int32.");
    }
    // The example displays the following output:
    //       163245617
    //       35902
    //       132451
    
    let lNumber = 163245617L
    try
        let number1 = int lNumber
        printfn $"{number1}"
    with :? OverflowException ->
        printfn $"{lNumber} is out of range of an Int32."
    
    let dbl2 = 35901.997
    try
        let number2 = int dbl2
        printfn $"{number2}"
    with :? OverflowException ->
        printfn $"{dbl2} is out of range of an Int32."
    
    let bigNumber = BigInteger 132451
    try
        let number3 = int bigNumber
        printfn $"{number3}"
    with :? OverflowException ->
        printfn $"{bigNumber} is out of range of an Int32."
    
    // The example displays the following output:
    //       163245617
    //       35902
    //       132451
    
    Dim lNumber As Long = 163245617
    Try
       Dim number1 As Integer = CInt(lNumber)
       Console.WriteLine(number1)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int32.", lNumber)
    End Try
    
    Dim dbl2 As Double = 35901.997
    Try
       Dim number2 As Integer = CInt(dbl2)
       Console.WriteLine(number2)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int32.", dbl2)
    End Try
       
    Dim bigNumber As BigInteger = 132451
    Try
       Dim number3 As Integer = CInt(bigNumber)
       Console.WriteLine(number3)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int32.", bigNumber)
    End Try    
    ' The example displays the following output:
    '       163245617
    '       35902
    '       132451
    
  • 您可以呼叫 Convert 類別的方法,將任何支援的型別轉換成 Int32 值。 這是可能的,因為 Int32 支援 IConvertible 介面。 下列範例說明如何將 Decimal 值陣列轉換成 Int32 值。

    decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m,
                        199.55m, 9214.16m, Decimal.MaxValue };
    int result;
    
    foreach (decimal value in values)
    {
       try {
          result = Convert.ToInt32(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 Int32 type.",
                            value);
       }
    }
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int32 type.
    //    Converted the Decimal value '-1034.23' to the Int32 value -1034.
    //    Converted the Decimal value '-12' to the Int32 value -12.
    //    Converted the Decimal value '0' to the Int32 value 0.
    //    Converted the Decimal value '147' to the Int32 value 147.
    //    Converted the Decimal value '199.55' to the Int32 value 200.
    //    Converted the Decimal value '9214.16' to the Int32 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int32 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.ToInt32 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 Int32 type."
       
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int32 type.
    //    Converted the Decimal value '-1034.23' to the Int32 value -1034.
    //    Converted the Decimal value '-12' to the Int32 value -12.
    //    Converted the Decimal value '0' to the Int32 value 0.
    //    Converted the Decimal value '147' to the Int32 value 147.
    //    Converted the Decimal value '199.55' to the Int32 value 200.
    //    Converted the Decimal value '9214.16' to the Int32 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int32 type.
    
    Dim values() As Decimal = { Decimal.MinValue, -1034.23d, -12d, 0d, 147d, _
                                199.55d, 9214.16d, Decimal.MaxValue }
    Dim result As Integer
    
    For Each value As Decimal In values
       Try
          result = Convert.ToInt32(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 Int32 type.", _
                            value)
       End Try   
    Next                                  
    ' The example displays the following output:
    '    -79228162514264337593543950335 is outside the range of the Int32 type.
    '    Converted the Decimal value '-1034.23' to the Int32 value -1034.
    '    Converted the Decimal value '-12' to the Int32 value -12.
    '    Converted the Decimal value '0' to the Int32 value 0.
    '    Converted the Decimal value '147' to the Int32 value 147.
    '    Converted the Decimal value '199.55' to the Int32 value 200.
    '    Converted the Decimal value '9214.16' to the Int32 value 9214.
    '    79228162514264337593543950335 is outside the range of the Int32 type.
    
  • 您可以呼叫 ParseTryParse 方法,將 Int32 值的字串表示轉換成 Int32。 字串可以包含十進位或十六進位數位。 下列範例說明使用十進位和十六進位字串來剖析作業。

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

對 Int32 值執行作業

Int32 類型支援標準數學運算,例如加法、減法、除法、乘法、負數和一元負數。 與其他整數類型一樣,Int32 類型也支援位元運算 ANDORXOR、左移和右移運算符。

您可以使用標準數值運算符來比較兩個 Int32 值,或呼叫 CompareToEquals 方法。

您也可以呼叫 Math 類別的成員來執行廣泛的數值運算,包括取得數字的絕對值、計算整數除數的商數和餘數、判斷兩個整數的最大值或最小值、取得數位的正負號,以及四捨五入數位。

將 Int32 表示為字串

Int32 類型提供標準和自定義數值格式字串的完整支援。 (如需詳細資訊,請參閱 格式化類型標準數值格式字串自定義數值格式字串

若要將 Int32 值格式化為不含前置零的整數位符串,您可以呼叫無參數 ToString() 方法。 藉由使用 「D」 格式規範,您也可以在字串表示中包含指定數目的前置零。 藉由使用 「N」 格式規範,您可以包含群組分隔符,並指定要出現在數位字串表示中的十進位數。 藉由使用 「X」 格式規範,您可以將 Int32 值表示為十六進位字串。 下列範例會以下列四種方式格式化 Int32 值陣列中的元素。

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


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

您也可以呼叫 Int32 方法,並提供基底做為方法的第二個參數,將 ToString(Int32, Int32) 值格式化為二進位、八進位、十六進位或十六進位字串。 下列範例會呼叫這個方法,以顯示整數值陣列的二進位、八進位和十六進位表示法。

int[] numbers = { -146, 11043, 2781913 };
Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}",
                  "Value", "Binary", "Octal", "Hex");
foreach (int number in numbers)
{
    Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}",
                      number, Convert.ToString(number, 2),
                      Convert.ToString(number, 8),
                      Convert.ToString(number, 16));
}
// The example displays the following output:
//       Value                             Binary         Octal          Hex
//        -146   11111111111111111111111101101110   37777777556     ffffff6e
//       11043                     10101100100011         25443         2b23
//     2781913             1010100111001011011001      12471331       2a72d9
let numbers = [| -146; 11043; 2781913 |]
printfn $"""{"Value",8}   {"Binary",32}   {"Octal",11}   {"Hex",10}""" 
for number in numbers do
    printfn $"{number,8}   {Convert.ToString(number, 2),32}   {Convert.ToString(number, 8),11}   {Convert.ToString(number, 16),10}"

// The example displays the following output:
//       Value                             Binary         Octal          Hex
//        -146   11111111111111111111111101101110   37777777556     ffffff6e
//       11043                     10101100100011         25443         2b23
//     2781913             1010100111001011011001      12471331       2a72d9
Dim numbers() As Integer = { -146, 11043, 2781913 }
Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}", _
                  "Value", "Binary", "Octal", "Hex")
For Each number As Integer In numbers
   Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}", _
                     number, Convert.ToString(number, 2), _
                     Convert.ToString(number, 8), _
                     Convert.ToString(number, 16))
Next      
' The example displays the following output:
'       Value                             Binary         Octal          Hex
'        -146   11111111111111111111111101101110   37777777556     ffffff6e
'       11043                     10101100100011         25443         2b23
'     2781913             1010100111001011011001      12471331       2a72d9

使用非十進制32位整數值

除了使用個別整數做為十進位值之外,您可能還想要使用整數值執行位運算,或是使用整數值的二進位或十六進位表示法。 Int32 值以 31 位表示,而使用 31 秒位做為符號位。 正數是使用符號和大小表示法來表示。 負值是用二進制補數表示的。 在對 Int32 值執行位運算或處理個別位時,請務必記住這點。 若要在任何兩個非十進位值上執行數值、布爾值或比較運算,這兩個值都必須使用相同的表示法。

欄位

名稱 Description
MaxValue

代表 的最大可能值 Int32。 此欄位是常數。

MinValue

代表最小 Int32的可能值。 此欄位是常數。

方法

名稱 Description
Abs(Int32)

計算值的絕對值。

BigMul(Int32, Int32)

產生兩個 32 位元數字的全積。

Clamp(Int32, Int32, Int32)

將值夾在內含最小值和最大值。

CompareTo(Int32)

將此實例與指定的 32 位元有符號整數比較,並回傳其相對值的指示。

CompareTo(Object)

比較這個實例與指定的物件,並傳回其相對值的指示。

CopySign(Int32, Int32)

將值的正負號複製到另一個值的正負號。

CreateChecked<TOther>(TOther)

從值建立目前型別的實例,針對落在目前類型可表示範圍以外的任何值擲回溢位例外狀況。

CreateSaturating<TOther>(TOther)

從值建立目前型別的實例,使落在目前類型可表示範圍以外的任何值飽和。

CreateTruncating<TOther>(TOther)

從值建立目前型別的實例,截斷任何落在目前類型可表示範圍以外的值。

DivRem(Int32, Int32)

計算兩個值的商數和餘數。

Equals(Int32)

回傳一個值,表示該實例是否等於指定 Int32 值。

Equals(Object)

傳回值,指出這個實例是否等於指定的物件。

GetHashCode()

傳回這個實例的哈希碼。

GetTypeCode()

回傳 TypeCode 的值型別 Int32

IsEvenInteger(Int32)

判斷值是否代表偶數整數。

IsNegative(Int32)

判斷值是否為負值。

IsOddInteger(Int32)

判斷值是否代表奇數整數。

IsPositive(Int32)

判斷值是否為正數。

IsPow2(Int32)

判斷值是否為兩個乘冪。

LeadingZeroCount(Int32)

計算值中的前置零數目。

Log10(Int32)

表示32位帶正負號的整數。

Log2(Int32)

計算 值的log2。

Max(Int32, Int32)

比較兩個值與計算,這兩個值更大。

MaxMagnitude(Int32, Int32)

比較兩個值與計算,這兩個值更大。

Min(Int32, Int32)

比較兩個值與計算比較較少。

MinMagnitude(Int32, Int32)

比較兩個值與計算比較較少。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式的數位範圍表示轉換為其32位帶正負號的整數對等。

Parse(String, IFormatProvider)

將指定之特定文化特性格式之數位的字串表示轉換為其相等的 32 位帶正負號整數。

Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數位的字串表示轉換為其相等的 32 位帶正負號整數。

Parse(String, NumberStyles)

將指定樣式中數位的字串表示轉換為其相等的32位帶正負號整數。

Parse(String)

將數位的字串表示轉換為其相等的32位帶正負號的整數。

PopCount(Int32)

計算值中所設定的位數。

RotateLeft(Int32, Int32)

以指定的數量旋轉值。

RotateRight(Int32, Int32)

以指定的數量向右旋轉值。

Sign(Int32)

計算值的符號。

ToString()

將這個實例的數值轉換為其相等的字串表示。

ToString(IFormatProvider)

利用指定的文化特定格式資訊,將此實例的數值轉換為其等效字串表示。

ToString(String, IFormatProvider)

使用指定的格式和文化特性特定格式資訊,將這個實例的數值轉換為其相等的字串表示。

ToString(String)

使用指定的格式,將這個實例的數值轉換為其相等的字串表示。

TrailingZeroCount(Int32)

計算值中尾端零的數目。

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

嘗試將目前實例的值格式化為UTF-8到提供的位元組範圍。

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

嘗試將當前整數實例的值格式化為提供的字元範圍。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int32)

嘗試將UTF-8字元的範圍剖析為值。

TryParse(ReadOnlySpan<Byte>, Int32)

嘗試將包含數位字串表示的UTF-8字元範圍轉換為其32位帶正負號的整數對等。

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

嘗試將UTF-8字元的範圍剖析為值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int32)

嘗試將字元範圍剖析成值。

TryParse(ReadOnlySpan<Char>, Int32)

將特定文化特性格式的數位範圍表示轉換為其相等的32位帶正負號整數。 傳回值表示轉換是否成功。

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

將指定樣式和特定文化特性格式的數位範圍表示轉換為其32位帶正負號的整數對等。 傳回值表示轉換是否成功。

TryParse(String, IFormatProvider, Int32)

嘗試將字串剖析成值。

TryParse(String, Int32)

將數位的字串表示轉換為其相等的32位帶正負號的整數。 傳回值表示轉換是否成功。

TryParse(String, NumberStyles, IFormatProvider, Int32)

將指定樣式和特定文化特性格式之數位的字串表示轉換為其相等的 32 位帶正負號整數。 傳回值表示轉換是否成功。

明確介面實作

名稱 Description
IAdditionOperators<Int32,Int32,Int32>.Addition(Int32, Int32)

將兩個值加在一起以計算其總和。

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

將兩個值加在一起以計算其總和。

IAdditiveIdentity<Int32,Int32>.AdditiveIdentity

取得目前型別的加法識別。

IBinaryInteger<Int32>.GetByteCount()

取得將被寫入為 TryWriteLittleEndian(Span<Byte>, Int32)的一部分的位元組數。

IBinaryInteger<Int32>.GetShortestBitLength()

取得最短兩個目前值的補碼表示長度,以位為單位。

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

表示32位帶正負號的整數。

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

表示32位帶正負號的整數。

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

嘗試以 big-endian 格式將目前的值寫入指定的範圍。

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

嘗試將目前值以小到尾格式寫入指定的範圍。

IBinaryNumber<Int32>.AllBitsSet

取得已設定所有位之二進位型別的實例。

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

計算兩個值的位和 。

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

計算兩個值的位或兩個值。

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

計算兩個值的獨佔或兩個值。

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

計算指定值的 ones-complement 表示法。

IComparable.CompareTo(Object)

比較這個實例與指定的物件,並傳回其相對值的指示。

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

比較兩個值,以判斷哪一個值更大。

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

比較兩個值,以判斷哪一個值大於或相等。

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

比較兩個值,以判斷哪一個值較少。

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

比較兩個值,以判斷哪一個值小於或相等。

IConvertible.GetTypeCode()

回傳 TypeCode 的值型別 Int32

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<Int32>.CheckedDecrement(Int32)

遞減值。

IDecrementOperators<Int32>.Decrement(Int32)

遞減值。

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

將一個值除以計算其商數。

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

比較兩個值以判斷相等。

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

比較兩個值以判斷不相等。

IIncrementOperators<Int32>.CheckedIncrement(Int32)

遞增值。

IIncrementOperators<Int32>.Increment(Int32)

遞增值。

IMinMaxValue<Int32>.MaxValue

取得目前型別的最大值。

IMinMaxValue<Int32>.MinValue

取得目前型別的最小值。

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

將兩個值分割在一起,以計算其模數或餘數。

IMultiplicativeIdentity<Int32,Int32>.MultiplicativeIdentity

取得目前型別的乘法識別。

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

將兩個值相乘以計算其產品。

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

將兩個值相乘以計算其產品。

INumber<Int32>.MaxNumber(Int32, Int32)

比較兩個值以計算較大的值,若輸入為 NaN則返回另一個值。

INumber<Int32>.MinNumber(Int32, Int32)

比較兩個值以計算一個較小的值,若輸入為 NaN則返回另一個值。

INumberBase<Int32>.IsCanonical(Int32)

判斷值是否在其標準表示法中。

INumberBase<Int32>.IsComplexNumber(Int32)

判斷值是否代表複數。

INumberBase<Int32>.IsFinite(Int32)

判斷值是否有限。

INumberBase<Int32>.IsImaginaryNumber(Int32)

判斷值是否代表純虛數。

INumberBase<Int32>.IsInfinity(Int32)

判斷值是否為無限。

INumberBase<Int32>.IsInteger(Int32)

判斷值是否代表整數。

INumberBase<Int32>.IsNaN(Int32)

判斷值是否為 NaN。

INumberBase<Int32>.IsNegativeInfinity(Int32)

判斷值是否為負無限大。

INumberBase<Int32>.IsNormal(Int32)

判斷值是否正常。

INumberBase<Int32>.IsPositiveInfinity(Int32)

判斷值是否為正無限大。

INumberBase<Int32>.IsRealNumber(Int32)

判斷值是否代表實數。

INumberBase<Int32>.IsSubnormal(Int32)

判斷值是否為次正規。

INumberBase<Int32>.IsZero(Int32)

判斷值是否為零。

INumberBase<Int32>.MaxMagnitudeNumber(Int32, Int32)

比較兩個值以計算出哪個大小較大,若輸入為 NaN則返回另一個值。

INumberBase<Int32>.MinMagnitudeNumber(Int32, Int32)

比較兩個值以計算大小較小的值,若輸入為 NaN則返回另一個值。

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

計算估計值為(left * right) + 。 addend

INumberBase<Int32>.One

取得該類型的數值 1

INumberBase<Int32>.Radix

取得型別的進位或基底。

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

表示32位帶正負號的整數。

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

表示32位帶正負號的整數。

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

表示32位帶正負號的整數。

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

嘗試將目前類型的實例轉換成另一個類型,針對落在目前類型可表示範圍以外的任何值擲回溢位例外狀況。

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

嘗試將目前類型的實例轉換成另一個類型,使任何落在目前類型可表示範圍以外的值飽和。

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

嘗試將目前類型的實例轉換成另一個類型,截斷落在目前類型可表示範圍以外的任何值。

INumberBase<Int32>.Zero

取得該類型的數值 0

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

將指定數量留下的值移位。

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

將值向右移位指定的數量。

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

將值向右移位指定的數量。

ISignedNumber<Int32>.NegativeOne

取得該類型的數值 -1

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

減去兩個值來計算其差異。

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

減去兩個值來計算其差異。

IUnaryNegationOperators<Int32,Int32>.CheckedUnaryNegation(Int32)

計算值的已核取一元否定。

IUnaryNegationOperators<Int32,Int32>.UnaryNegation(Int32)

計算值的一元負值。

IUnaryPlusOperators<Int32,Int32>.UnaryPlus(Int32)

計算值的一元加號。

適用於

執行緒安全性

此類型的所有成員都是安全線程。 看似修改實例狀態的成員實際上會傳回以新值初始化的新實例。 如同任何其他類型,讀取和寫入包含此類型實例的共用變數必須受到鎖定的保護,以確保線程安全性。

另請參閱