Int32 構造体

定義

32 ビット符号付き整数を表します。

public value class int : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
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<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, 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, 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>
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>
[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 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 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 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, IConvertible, IFormattable
Public Structure Int32
Implements IComparable, IComparable(Of Integer), IEquatable(Of Integer), IFormattable
継承
属性
実装
IComparable IComparable<Int32> IConvertible IEquatable<Int32> IFormattable ISpanFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Int32> IParsable<TSelf> ISpanParsable<Int32> ISpanParsable<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> IUtf8SpanFormattable IUtf8SpanParsable<Int32> IUtf8SpanParsable<TSelf>

注釈

Int32 は、負の 2,147,483,648 (定数で Int32.MinValue 表される) から正の 2,147,483,647 (定数で Int32.MaxValue 表される) までの範囲の値を持つ符号付き整数を表す不変の値型です。 .NET には、0 ~ 4,294,967,295 の範囲の値を表す符号なし 32 ビット整数値型 UInt32も含まれています。

Int32 値のインスタンス化

値は、いくつかの方法でインスタンス化 Int32 できます。

  • 変数を Int32 宣言し、データ型の範囲内にあるリテラル整数値を Int32 割り当てることができます。 次の例では、2 つの 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# のキャスト演算子と、Visual Basic の変換メソッド (オンの場合 Option Strict ) が必要です。 数値が小数部を Single含む 、 Double、または Decimal の値の場合、その小数部の処理は、変換を実行するコンパイラによって異なります。 次の例では、縮小変換を実行して、複数の数値を変数に Int32 割り当てます。

    long lNumber = 163245617;
    try {
       int number1 = (int) lNumber;
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int32.", lNumber);
    }
    
    double dbl2 = 35901.997;
    try {
       int number2 = (int) dbl2;
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int32.", dbl2);
    }
    
    BigInteger bigNumber = 132451;
    try {
       int number3 = (int) bigNumber;
       Console.WriteLine(number3);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int32.", bigNumber);
    }
    // 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 値に変換できます。 これは、 インターフェイスをサポートしているため Int32IConvertible 可能です。 次の例は、値の配列 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.
    
  • または メソッドをParse呼び出して、値の文字列形式を Int32Int32変換TryParseできます。 文字列には、10 進数または 16 進数を含めることができます。 次の例は、10 進文字列と 16 進数文字列の両方を使用した解析操作を示しています。

    string string1 = "244681";
    try {
       int number1 = Int32.Parse(string1);
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string1);
    }
    catch (FormatException) {
       Console.WriteLine("The format of '{0}' is invalid.", string1);
    }
    
    string string2 = "F9A3C";
    try {
       int number2 = Int32.Parse(string2,
                                System.Globalization.NumberStyles.HexNumber);
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string2);
    }
    catch (FormatException) {
       Console.WriteLine("The format of '{0}' is invalid.", string2);
    }
    // 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、 型は、ビットごとの AND、、ORXOR、左シフト、および右シフト演算子もサポートしています。

標準の数値演算子を使用して 2 つのInt32値を比較するか、 メソッドまたは Equals メソッドをCompareTo呼び出すことができます。

クラスのメンバーを呼び出して、数値の Math 絶対値の取得、整数除算からの商と剰余の計算、2 つの整数の最大値または最小値の決定、数値の符号の取得、数値の丸めなど、さまざまな数値演算を実行することもできます。

Int32 を文字列として表す

型は Int32 、標準およびカスタムの数値書式指定文字列を完全にサポートします。 (詳細については、「 書式指定型」、「 標準数値書式指定文字列」、「 カスタム数値書式指定文字列」を参照してください)。

先頭に Int32 ゼロを付けずに整数文字列として値を書式設定するには、パラメーターなしの ToString() メソッドを呼び出します。 "D" 書式指定子を使用すると、文字列表現に指定した数の先行ゼロを含めることもできます。 "N" 書式指定子を使用すると、グループ区切り記号を含め、数値の文字列表現に表示する 10 進数の数を指定できます。 "X" 書式指定子を使用すると、値を Int32 16 進数の文字列として表すことができます。 次の例では、これらの 4 つの方法で値の配列内の 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

また、 メソッドを呼び出しToString(Int32, Int32)、基底をInt32メソッドの 2 番目のパラメーターとして指定することで、値をバイナリ、8 進数、10 進数、または 16 進数の文字列として書式設定することもできます。 次の例では、このメソッドを呼び出して、整数値の配列のバイナリ、8 進数、および 16 進数の表現を表示します。

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

10 進数以外の 32 ビット整数値の操作

個々の整数を 10 進値として操作するだけでなく、整数値を使用してビットごとの操作を実行したり、整数値のバイナリ表現または 16 進数表現を操作したりできます。 Int32 値は 31 ビットで表され、30 秒のビットが符号ビットとして使用されます。 正の値は、符号と大きさの表現を使用して表されます。 負の値は、2 の補数表現にあります。 これは、値に対してビットごとの操作を実行する場合、または個々のビットを Int32 操作する場合に注意することが重要です。 2 つの 10 進数以外の値に対して数値、ブール値、または比較演算を実行するには、両方の値で同じ表現を使用する必要があります。

フィールド

MaxValue

Int32 の最大有効値を表します。 このフィールドは定数です。

MinValue

Int32 の最小有効値を表します。 このフィールドは定数です。

メソッド

Abs(Int32)

値の絶対を計算します。

Clamp(Int32, Int32, Int32)

値を包括的な最小値と最大値にクランプします。

CompareTo(Int32)

指定した 32 ビット符号付き整数とこのインスタンスを比較し、これらの相対値を示す値を返します。

CompareTo(Object)

指定したオブジェクトとこのインスタンスを比較し、これらの相対値を示す値を返します。

CopySign(Int32, Int32)

値の符号を別の値の符号にコピーします。

CreateChecked<TOther>(TOther)

現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外にある値に対してオーバーフロー例外をスローします。

CreateSaturating<TOther>(TOther)

現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を飽和させます。

CreateTruncating<TOther>(TOther)

現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を切り捨てます。

DivRem(Int32, Int32)

2 つの値の商と剰余を計算します。

Equals(Int32)

このインスタンスが指定した Int32 値に等しいかどうかを示す値を返します。

Equals(Object)

このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。

GetHashCode()

このインスタンスのハッシュ コードを返します。

GetTypeCode()

Int32 値型の TypeCode を返します。

IsEvenInteger(Int32)

値が偶数の整数を表すかどうかを判断します。

IsNegative(Int32)

値が負かどうかを判断します。

IsOddInteger(Int32)

値が奇数の整数を表すかどうかを判断します。

IsPositive(Int32)

値が正かどうかを判断します。

IsPow2(Int32)

値が 2 の累乗であるかどうかを判断します。

LeadingZeroCount(Int32)

値の先頭の 0 の数を計算します。

Log2(Int32)

値の log2 を計算します。

Max(Int32, Int32)

2 つの値を比較して、より大きいコンピューティングを行います。

MaxMagnitude(Int32, Int32)

2 つの値を比較して、より大きいコンピューティングを行います。

Min(Int32, Int32)

2 つの値を比較して、より小さいコンピューティングを行います。

MinMagnitude(Int32, Int32)

2 つの値を比較して、より小さいコンピューティングを行います。

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)

数値の文字列形式を、それと等価な 32 ビット符号付き整数に変換します。

Parse(String, IFormatProvider)

指定したカルチャに固有の書式による数値の文字列形式を、それと等価な 32 ビット符号付き整数に変換します。

Parse(String, NumberStyles)

指定したスタイルの数値の文字列形式を、それと等しい 32 ビット符号付き整数に変換します。

Parse(String, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 32 ビット符号付き整数に変換します。

PopCount(Int32)

値に設定されているビット数を計算します。

RotateLeft(Int32, Int32)

指定した量だけ値を左に回転します。

RotateRight(Int32, Int32)

指定した量だけ値を右方向に回転します。

Sign(Int32)

値の符号を計算します。

ToString()

このインスタンスの数値を、それと等価な文字列形式に変換します。

ToString(IFormatProvider)

このインスタンスの数値を、指定したカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。

ToString(String)

指定した書式を使用して、このインスタンスの数値を、それと等価な文字列形式に変換します。

ToString(String, IFormatProvider)

このインスタンスの数値を、指定した書式およびカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。

TrailingZeroCount(Int32)

値の末尾の 0 の数を計算します。

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 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。

明示的なインターフェイスの実装

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

合計を計算するために 2 つの値を加算します。

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

合計を計算するために 2 つの値を加算します。

IAdditiveIdentity<Int32,Int32>.AdditiveIdentity

現在の型の加算 ID を取得します。

IBinaryInteger<Int32>.GetByteCount()

の一部として書き込まれるバイト数を TryWriteLittleEndian(Span<Byte>, Int32)取得します。

IBinaryInteger<Int32>.GetShortestBitLength()

現在の値の最短 2 の補数表現の長さをビット単位で取得します。

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

32 ビット符号付き整数を表します。

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

32 ビット符号付き整数を表します。

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

現在の値をビッグ エンディアン形式で特定のスパンに書き込もうとします。

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

特定のスパンに現在の値をリトル エンディアン形式で書き込もうとします。

IBinaryNumber<Int32>.AllBitsSet

すべてのビットが設定されているバイナリ型のインスタンスを取得します。

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

ビットごとの と の 2 つの値を計算します。

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

ビットごとのまたは 2 つの値を計算します。

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

2 つの値の排他的または排他的を計算します。

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

指定された値の 1-補数表現を計算します。

IComparable.CompareTo(Object)

指定したオブジェクトとこのインスタンスを比較し、これらの相対値を示す値を返します。

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

2 つの値を比較して、どちらが大きいかを判断します。

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

2 つの値を比較して、どちらが大きいか等しいかを判断します。

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

2 つの値を比較して、どちらが小さいかを判断します。

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

2 つの値を比較して、次の値以下を決定します。

IConvertible.GetTypeCode()

Int32 値型の TypeCode を返します。

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)

1 つの値を別の値で除算して商を計算します。

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

2 つの値を比較して等価性を判断します。

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

2 つの値を比較して、不等値を判断します。

IIncrementOperators<Int32>.CheckedIncrement(Int32)

値をインクリメントします。

IIncrementOperators<Int32>.Increment(Int32)

値をインクリメントします。

IMinMaxValue<Int32>.MaxValue

現在の型の最大値を取得します。

IMinMaxValue<Int32>.MinValue

現在の型の最小値を取得します。

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

2 つの値を一緒に除算して、その剰余または剰余を計算します。

IMultiplicativeIdentity<Int32,Int32>.MultiplicativeIdentity

現在の型の乗法 ID を取得します。

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

2 つの値を乗算して製品を計算します。

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

2 つの値を乗算して製品を計算します。

INumber<Int32>.MaxNumber(Int32, Int32)

2 つの値を比較して、より大きいを計算し、入力が の場合は NaNもう一方の値を返します。

INumber<Int32>.MinNumber(Int32, Int32)

2 つの値を比較して、より小さい計算を行い、入力が の場合は 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)

値が 0 かどうかを判断します。

INumberBase<Int32>.MaxMagnitudeNumber(Int32, Int32)

2 つの値を比較して、大きさが大きい計算を行い、入力が の場合は NaNもう一方の値を返します。

INumberBase<Int32>.MinMagnitudeNumber(Int32, Int32)

2 つの値を比較して、大きさが小さい計算を行い、入力が の場合は NaNもう一方の値を返します。

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)

2 つの値を減算して差を計算します。

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

2 つの値を減算して差を計算します。

IUnaryNegationOperators<Int32,Int32>.CheckedUnaryNegation(Int32)

値のチェックされた単項否定を計算します。

IUnaryNegationOperators<Int32,Int32>.UnaryNegation(Int32)

値の単項否定を計算します。

IUnaryPlusOperators<Int32,Int32>.UnaryPlus(Int32)

値の単項プラスを計算します。

適用対象

スレッド セーフ

この型のすべてのメンバーはスレッド セーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッドセーフを保証するためにロックによって保護する必要があります。

こちらもご覧ください