# Int32Int32Int32Int32 Struct

## 定義

32 ビット符号付き整数を表します。Represents a 32-bit signed integer.

``public value class Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable``
``````[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable``````
``````type int = struct
interface IFormattable
interface IConvertible``````
``````Public Structure Int32
Implements IComparable, IComparable(Of Integer), IConvertible, IEquatable(Of Integer), IFormattable``````

Int32Int32Int32Int32

## 注釈

Int32は、負の 2147483648 ( Int32.MinValue定数で表されます) から正の 2147483647 ( Int32.MaxValue定数で表される) までの値を持つ符号付き整数を表す、変更できない値型です。Int32 is an immutable value type that represents signed integers with values that range from negative 2,147,483,648 (which is represented by the Int32.MinValue constant) through positive 2,147,483,647 (which is represented by the Int32.MaxValue constant. .NET Framework には、0 ~ 4294967295 の範囲の値を表すUInt32、符号なし32ビット整数値型 () も含まれています。The .NET Framework also includes an unsigned 32-bit integer value type, UInt32, which represents values that range from 0 to 4,294,967,295.

### Int32 値のインスタンス化Instantiating an Int32 Value

• Int32変数を宣言し、 Int32データ型の範囲内のリテラル整数値を割り当てることができます。You can declare an Int32 variable and assign it a literal integer value that is within the range of the Int32 data type. 次の例ではInt32 、2つの変数を宣言し、その値をこのように代入します。The following example declares two Int32 variables and assigns them values in this way.

``````int number1 = 64301;
int number2 = 25548612;
``````
``````Dim number1 As Integer = 64301
Dim number2 As Integer = 25548612
``````
• 範囲がInt32型のサブセットである整数型の値を割り当てることができます。You can assign the value of an integer type whose range is a subset of the Int32 type. これは、のC#キャスト演算子や Visual Basic の変換メソッドを必要としない拡大変換です。This is a widening conversion that does not require a cast operator in C# or a conversion method in Visual Basic.

``````sbyte value1 = 124;
short value2 = 1618;

int number1 = value1;
int number2 = value2;
``````
``````Dim value1 As SByte = 124
Dim value2 As Int16 = 1618

Dim number1 As Integer = value1
Dim number2 As Integer = value2
``````
• 範囲がInt32型の値を超えている数値型の値を割り当てることができます。You can assign the value of a numeric type whose range exceeds that of the Int32 type. これは縮小変換であるため、がオンになってC#いる場合`Option Strict`は、のキャスト演算子と Visual Basic の変換メソッドが必要です。This is a narrowing conversion, so it requires a cast operator in C# and a conversion method in Visual Basic if `Option Strict` is on. 数値がSingle、小数部分を含むDouble、、 Decimalまたはの値の場合、その小数部の処理は、変換を実行するコンパイラによって異なります。If the numeric value is a Single, Double, or Decimal value that includes a fractional component, the handling of its fractional part depends on the compiler performing the conversion. 次の例では、縮小変換を実行してInt32 、複数の数値を変数に割り当てています。The following example performs narrowing conversions to assign several numeric values to Int32 variables.

``````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
``````
``````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を値に変換できます。You can call a method of the Convert class to convert any supported type to an Int32 value. ではインターフェイスがInt32サポートさIConvertibleれているため、これが可能です。This is possible because Int32 supports the IConvertible interface. 次の例は、値のDecimal配列を値に変換する方法をInt32示しています。The following example illustrates the conversion of an array of Decimal values to Int32 values.

``````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.
``````
``````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またはTryParseメソッドを呼び出して、 Int32Int32の文字列形式をに変換できます。You can call the Parse or TryParse method to convert the string representation of an Int32 value to an Int32. 文字列には、10進数または16進数のいずれかを含めることができます。The string can contain either decimal or hexadecimal digits. 次の例は、10進数と16進数の文字列の両方を使用した解析操作を示しています。The following example illustrates the parse operation by using both a decimal and a hexadecimal string.

``````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
``````
``````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 値に対する操作の実行Performing Operations on Int32 Values

Int32は、加算、減算、除算、乗算、否定、単項否定などの標準的な数値演算をサポートします。The Int32 type supports standard mathematical operations such as addition, subtraction, division, multiplication, negation, and unary negation. 他の整数型と同様にInt32 、型では、 `AND`ビット`OR`ごと`XOR`の、、、左シフト、および右シフト演算子もサポートされています。Like the other integral types, the Int32 type also supports the bitwise `AND`, `OR`, `XOR`, left shift, and right shift operators.

また、 Mathクラスのメンバーを呼び出して、数値の絶対値を取得したり、整数除算から商と剰余を計算したり、最大値または最小値を決定したりするなど、さまざまな数値演算を実行することもできます。整数。数値の符号を取得し、数値を丸めます。You can also call the members of the Math class to perform a wide range of numeric operations, including getting the absolute value of a number, calculating the quotient and remainder from integral division, determining the maximum or minimum value of two integers, getting the sign of a number, and rounding a number.

### Int32 を文字列として表すRepresenting an Int32 as a String

Int32は、標準およびカスタムの数値書式指定文字列を完全にサポートします。The Int32 type provides full support for standard and custom numeric format strings. (詳細については、「書式設定型」、「標準の数値書式指定文字列」、および「カスタム数値書式指定文字列」を参照してください)。(For more information, see Formatting Types, Standard Numeric Format Strings, and Custom Numeric Format Strings.)

Int32値を、先行ゼロを含まない整数文字列として書式設定するにはToString() 、パラメーターなしのメソッドを呼び出すことができます。To format an Int32 value as an integral string with no leading zeros, you can call the parameterless ToString() method. "D" 書式指定子を使用すると、指定した数の先行ゼロを文字列形式で含めることもできます。By using the "D" format specifier, you can also include a specified number of leading zeros in the string representation. "N" 書式指定子を使用すると、桁区切り記号を含めたり、数値の文字列形式で表示する小数点以下の桁数を指定したりすることができます。By using the "N" format specifier, you can include group separators and specify the number of decimal digits to appear in the string representation of the number. "X" 書式指定子を使用すると、 Int32値を16進文字列として表すことができます。By using the "X" format specifier, you can represent an Int32 value as a hexadecimal string. 次の例では、これらの4つInt32の方法で、値の配列の要素の書式を設定します。The following example formats the elements in an array of Int32 values in these four ways.

``````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);
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
``````
``````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)
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番目のパラメーターとして base を指定することで、バイナリ、8進数、10進数、または16進数の文字列として値を書式設定することもできます。You can also format an Int32 value as a binary, octal, decimal, or hexadecimal string by calling the ToString(Int32, Int32) method and supplying the base as the method's second parameter. 次の例では、このメソッドを呼び出して、整数値の配列のバイナリ、8進数、および16進数表現を表示します。The following example calls this method to display the binary, octal, and hexadecimal representations of an array of integer values.

``````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
``````
``````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
``````

## フィールド

 MaxValue MaxValue MaxValue MaxValue Int32 の最大有効値を表します。Represents the largest possible value of an Int32. このフィールドは定数です。This field is constant. MinValue MinValue MinValue MinValue Int32 の最小有効値を表します。Represents the smallest possible value of Int32. このフィールドは定数です。This field is constant.

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

 IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object) IConvertible.GetTypeCode() IConvertible.GetTypeCode() IConvertible.GetTypeCode() IConvertible.GetTypeCode() IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) このメンバーの詳細については、「ToBoolean(IFormatProvider)」をご覧ください。For a description of this member, see ToBoolean(IFormatProvider). IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) このメンバーの説明については、ToByte(IFormatProvider) のトピックを参照してください。For a description of this member, see ToByte(IFormatProvider). IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) このメンバーの説明については、ToChar(IFormatProvider) のトピックを参照してください。For a description of this member, see ToChar(IFormatProvider). IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) この変換はサポートされていません。This conversion is not supported. このメソッドを使用しようとすると、InvalidCastException がスローされます。Attempting to use this method throws an InvalidCastException. IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) このメンバーの詳細については、「ToDecimal(IFormatProvider)」をご覧ください。For a description of this member, see ToDecimal(IFormatProvider). IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) このメンバーの説明については、ToDouble(IFormatProvider) のトピックを参照してください。For a description of this member, see ToDouble(IFormatProvider). IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) このメンバーの詳細については、「ToInt16(IFormatProvider)」をご覧ください。For a description of this member, see ToInt16(IFormatProvider). IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) このメンバーの詳細については、「ToInt32(IFormatProvider)」をご覧ください。For a description of this member, see ToInt32(IFormatProvider). IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) このメンバーの詳細については、「ToInt64(IFormatProvider)」をご覧ください。For a description of this member, see ToInt64(IFormatProvider). IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) このメンバーの説明については、ToSByte(IFormatProvider) のトピックを参照してください。For a description of this member, see ToSByte(IFormatProvider). IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) このメンバーの詳細については、「ToSingle(IFormatProvider)」をご覧ください。For a description of this member, see ToSingle(IFormatProvider). IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) このメンバーの詳細については、「ToType(Type, IFormatProvider)」をご覧ください。For a description of this member, see ToType(Type, IFormatProvider). IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) このメンバーの説明については、ToUInt16(IFormatProvider) のトピックを参照してください。For a description of this member, see ToUInt16(IFormatProvider). IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) このメンバーの説明については、ToUInt32(IFormatProvider) のトピックを参照してください。For a description of this member, see ToUInt32(IFormatProvider). IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) このメンバーの詳細については、「ToUInt64(IFormatProvider)」をご覧ください。For a description of this member, see ToUInt64(IFormatProvider).

## スレッド セーフ

この型のすべてのメンバーは、スレッドセーフです。All members of this type are thread safe. インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。Members that appear to modify instance state actually return a new instance initialized with the new value. 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッドセーフを保証するためにロックによって保護される必要があります。As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.