Int32 Struktura
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Reprezentuje 32-bitową liczbę całkowitą ze znakiem.
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>, System::Numerics::IDecrementOperators<int>, System::Numerics::IDivisionOperators<int, int, int>, System::Numerics::IEqualityOperators<int, int>, 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>, 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>, System.Numerics.IDecrementOperators<int>, System.Numerics.IDivisionOperators<int,int,int>, System.Numerics.IEqualityOperators<int,int>, 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>, 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>
interface IEqualityOperators<int, int>
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>
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), IConvertible, IDecrementOperators(Of Integer), IDivisionOperators(Of Integer, Integer, Integer), IEqualityOperators(Of Integer, Integer), 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), 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
- Dziedziczenie
- Atrybuty
- Implementuje
-
IComparable IComparable<Int32> IConvertible IEquatable<Int32> IFormattable ISpanFormattable IComparable<TOther> IComparable<TSelf> IEquatable<TOther> 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> System.Numerics.IComparisonOperators<Int32,Int32> System.Numerics.IComparisonOperators<TSelf,TSelf> IDecrementOperators<Int32> IDecrementOperators<TSelf> IDivisionOperators<Int32,Int32,Int32> IDivisionOperators<TSelf,TSelf,TSelf> System.Numerics.IEqualityOperators<Int32,Int32> System.Numerics.IEqualityOperators<TSelf,TOther> System.Numerics.IEqualityOperators<TSelf,TSelf> 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> System.Numerics.IShiftOperators<Int32,Int32> System.Numerics.IShiftOperators<TSelf,TSelf> ISignedNumber<Int32> ISubtractionOperators<Int32,Int32,Int32> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<Int32,Int32> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Int32,Int32> IUnaryPlusOperators<TSelf,TSelf>
Uwagi
Int32 jest niezmiennym typem wartości, który reprezentuje podpisane liczby całkowite z wartościami, które wahają się od ujemnych 2147 483 648 (co jest reprezentowane przez Int32.MinValue stałą) przez dodatnie 2,147,483,647 (co jest reprezentowane przez stałą Int32.MaxValue . Platforma .NET zawiera również niepodpisany typ wartości 32-bitowej liczby całkowitej, UInt32który reprezentuje wartości z zakresu od 0 do 4294 967 295.
Utworzenie wystąpienia wartości Int32
Wystąpienie wartości można utworzyć Int32 na kilka sposobów:
Możesz zadeklarować zmienną Int32 i przypisać jej wartość całkowitą literału, która znajduje się w zakresie Int32 typu danych. Poniższy przykład deklaruje dwie Int32 zmienne i przypisuje im wartości w ten sposób.
int number1 = 64301; int number2 = 25548612;
let number1 = 64301 let number2 = 25548612
Dim number1 As Integer = 64301 Dim number2 As Integer = 25548612
Można przypisać wartość typu liczby całkowitej, którego zakres jest podzbiorem Int32 typu. Jest to konwersja rozszerzająca, która nie wymaga operatora rzutowania w języku C# ani metody konwersji w języku Visual Basic, ale wymaga operatora w języku 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
Można przypisać wartość typu liczbowego, którego zakres przekracza ten Int32 typ. Jest to konwersja zawężająca, więc wymaga operatora rzutowania w języku C# lub F#oraz metody konwersji w języku Visual Basic, jeśli
Option Strict
jest włączona. Jeśli wartość liczbowa jest wartością Single, Doublelub Decimal zawierającą składnik ułamkowy, obsługa jego części ułamkowej zależy od kompilatora wykonującego konwersję. Poniższy przykład wykonuje konwersje zawężające w celu przypisania kilku wartości liczbowych do Int32 zmiennych.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
Możesz wywołać metodę Convert klasy, aby przekonwertować dowolny obsługiwany typ na Int32 wartość. Jest to możliwe, ponieważ Int32 obsługuje IConvertible interfejs. Poniższy przykład ilustruje konwersję tablicy Decimal wartości na Int32 wartości.
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.
Możesz wywołać metodę Parse lub TryParse , aby przekonwertować reprezentację Int32 ciągu wartości na Int32wartość . Ciąg może zawierać cyfry dziesiętne lub szesnastkowe. Poniższy przykład ilustruje operację analizy przy użyciu zarówno dziesiętnego, jak i ciągu szesnastkowego.
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
Wykonywanie operacji na wartościach Int32
Typ Int32 obsługuje standardowe operacje matematyczne, takie jak dodawanie, odejmowanie, dzielenie, mnożenie, negacja i negacja jednoargumentowa. Podobnie jak w przypadku innych typów całkowitych, Int32 typ obsługuje również operatory bitowe AND
, OR
, , XOR
shift w lewo i przesunięcia w prawo.
Możesz użyć standardowych operatorów liczbowych, aby porównać dwie Int32 wartości lub wywołać metodę CompareTo lub Equals .
Można również wywołać elementy członkowskie Math klasy, aby wykonać szeroki zakres operacji liczbowych, w tym uzyskać wartość bezwzględną liczby, obliczyć iloraz i resztę z dzielenia całkowitego, określając maksymalną lub minimalną wartość dwóch liczb całkowitych, uzyskując znak liczby i zaokrąglając liczbę.
Reprezentowanie wartości Int32 jako ciągu
Typ Int32 zapewnia pełną obsługę ciągów standardowych i niestandardowych formatów liczbowych. (Aby uzyskać więcej informacji, zobacz Typy formatowania, Standardowe ciągi formatu liczbowego i Niestandardowe ciągi formatu liczbowego).
Aby sformatować Int32 wartość jako ciąg całkowity bez zer wiodących, możesz wywołać metodę bez ToString() parametrów. Używając specyfikatora formatu "D", można również uwzględnić określoną liczbę zer wiodących w reprezentacji ciągu. Używając specyfikatora formatu "N", można uwzględnić separatory grup i określić liczbę cyfr dziesiętnych, które mają być wyświetlane w ciągu reprezentującym liczbę. Za pomocą specyfikatora formatu "X" można reprezentować Int32 wartość jako ciąg szesnastkowy. Poniższy przykład formatuje elementy w tablicy Int32 wartości na te cztery sposoby.
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
Wartość można również sformatować Int32 jako ciąg binarny, ósemkowy, dziesiętny lub szesnastkowy, wywołując ToString(Int32, Int32) metodę i podając bazę jako drugi parametr metody. Poniższy przykład wywołuje tę metodę, aby wyświetlić reprezentacje binarne, ósemkowe i szesnastkowe tablicy wartości całkowitych.
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
Posługiwanie się niedziesiętnymi 32-bitowymi liczbami całkowitymi
Oprócz pracy z poszczególnymi liczbami całkowitymi jako wartościami dziesiętnymi można wykonać operacje bitowe z wartościami całkowitymi lub pracować z reprezentacjami binarnymi lub szesnastkowymi wartościami całkowitymi. Int32 wartości są reprezentowane w 31 bitach, a trzydziestosekundowy bit używany jako bit znaku. Wartości dodatnie są reprezentowane przy użyciu reprezentacji znaku i wielkości. Wartości ujemne znajdują się w reprezentacji dwóch uzupełnień. Ważne jest, aby pamiętać o wykonywaniu operacji bitowych na Int32 wartościach lub podczas pracy z poszczególnymi bitami. Aby wykonać operację liczbową, logiczną lub porównawczą na dwóch wartościach innych niż dziesiętne, obie wartości muszą używać tej samej reprezentacji.
Pola
MaxValue |
Reprezentuje największą możliwą wartość elementu Int32. To pole jest stałe. |
MinValue |
Reprezentuje najmniejszą możliwą wartość .Int32 To pole jest stałe. |
Metody
Abs(Int32) |
Oblicza wartość bezwzględną. |
Clamp(Int32, Int32, Int32) |
Zaciska wartość do wartości minimalnej i maksymalnej. |
CompareTo(Int32) |
Porównuje to wystąpienie z 32-bitową liczbą całkowitą ze znakiem i zwraca wskazanie ich wartości względnych. |
CompareTo(Object) |
Porównuje to wystąpienie z określonym obiektem i zwraca wskazanie ich względnych wartości. |
CopySign(Int32, Int32) |
Kopiuje znak wartości do znaku innej wartości. |
CreateChecked<TOther>(TOther) |
Tworzy wystąpienie bieżącego typu na podstawie wartości, zgłaszając wyjątek przepełnienia dla wszystkich wartości, które wykraczają poza dopuszczalny zakres bieżącego typu. |
CreateSaturating<TOther>(TOther) |
Tworzy wystąpienie bieżącego typu na podstawie wartości, usytując wszystkie wartości, które wykraczają poza reprezentowany zakres bieżącego typu. |
CreateTruncating<TOther>(TOther) |
Tworzy wystąpienie bieżącego typu na podstawie wartości, obcinając wszystkie wartości, które znajdują się poza dopuszczalnym zakresem bieżącego typu. |
DivRem(Int32, Int32) |
Oblicza iloraz i pozostałą część dwóch wartości. |
Equals(Int32) |
Zwraca wartość wskazującą, czy to wystąpienie jest równe określonej Int32 wartości. |
Equals(Object) |
Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi. |
GetHashCode() |
Zwraca wartość skrótu dla tego wystąpienia. |
GetTypeCode() | |
IsEvenInteger(Int32) |
Określa, czy wartość reprezentuje liczbę całkowitą parzystą. |
IsNegative(Int32) |
Określa, czy wartość jest ujemna. |
IsOddInteger(Int32) |
Określa, czy wartość reprezentuje nieparzystą liczbę całkowitą. |
IsPositive(Int32) |
Określa, czy wartość jest dodatnia. |
IsPow2(Int32) |
Określa, czy wartość jest potęgą dwóch. |
LeadingZeroCount(Int32) |
Oblicza liczbę zer wiodących w wartości. |
Log2(Int32) |
Oblicza dziennik2 wartości. |
Max(Int32, Int32) |
Porównuje dwie wartości do obliczeń, które są większe. |
MaxMagnitude(Int32, Int32) |
Porównuje dwie wartości do obliczeń, które są większe. |
Min(Int32, Int32) |
Porównuje dwie wartości do obliczeń, które są mniejsze. |
MinMagnitude(Int32, Int32) |
Porównuje dwie wartości do obliczeń, które są mniejsze. |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
Analizuje zakres znaków w wartości. |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na odpowiednik 32-bitowej liczby całkowitej ze znakiem. |
Parse(String) |
Konwertuje reprezentację ciągu liczby na 32-bitową liczbę całkowitą ze znakiem. |
Parse(String, IFormatProvider) |
Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na odpowiednik 32-bitowej liczby całkowitej ze znakiem. |
Parse(String, NumberStyles) |
Konwertuje reprezentację ciągu liczby w określonym stylu na odpowiednik 32-bitowej liczby całkowitej ze znakiem. |
Parse(String, NumberStyles, IFormatProvider) |
Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na odpowiednik 32-bitowej liczby całkowitej ze znakiem. |
PopCount(Int32) |
Oblicza liczbę bitów ustawionych w wartości. |
RotateLeft(Int32, Int32) |
Obraca wartość pozostawioną przez daną kwotę. |
RotateRight(Int32, Int32) |
Obraca wartość z prawej strony według danej kwoty. |
Sign(Int32) |
Oblicza znak wartości. |
ToString() |
Konwertuje wartość liczbową tego wystąpienia na równoważną reprezentację w postaci ciągu. |
ToString(IFormatProvider) |
Konwertuje wartość liczbową tego wystąpienia na równoważną reprezentację w postaci ciągu przy użyciu podanych danych formatowania specyficznych dla kultury. |
ToString(String) |
Konwertuje wartość liczbową tego wystąpienia na równoważną reprezentację w postaci ciągu przy użyciu podanego formatu. |
ToString(String, IFormatProvider) |
Konwertuje wartość liczbową tego wystąpienia na równoważną reprezentację w postaci ciągu przy użyciu podanego formatu i informacji specyficznych dla kultury. |
TrailingZeroCount(Int32) |
Oblicza liczbę końcowych zer w wartości. |
TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
Próbuje sformatować wartość bieżącej liczby całkowitej wystąpienia w podanym zakresie znaków. |
TryParse(ReadOnlySpan<Char>, IFormatProvider, Int32) |
Próbuje przeanalizować zakres znaków w wartości. |
TryParse(ReadOnlySpan<Char>, Int32) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na odpowiednik liczby całkowitej ze znakiem 32-bitowym. Wartość zwracana wskazuje, czy konwersja zakończyła się pomyślnie. |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int32) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na odpowiednik liczby całkowitej ze znakiem 32-bitowym. Wartość zwracana wskazuje, czy konwersja zakończyła się pomyślnie. |
TryParse(String, IFormatProvider, Int32) | |
TryParse(String, Int32) |
Konwertuje reprezentację ciągu liczby na 32-bitową liczbę całkowitą ze znakiem 32-bitowym. Wartość zwracana wskazuje, czy konwersja zakończyła się pomyślnie. |
TryParse(String, NumberStyles, IFormatProvider, Int32) |
Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na odpowiednik 32-bitowej liczby całkowitej ze znakiem. Wartość zwracana wskazuje, czy konwersja zakończyła się pomyślnie. |
Jawne implementacje interfejsu
IBinaryInteger<Int32>.GetByteCount() |
Pobiera liczbę bajtów, które zostaną zapisane w ramach TryWriteLittleEndian(Span<Byte>, Int32)elementu . |
IBinaryInteger<Int32>.GetShortestBitLength() |
Pobiera długość w bitach najkrótszej reprezentacji bieżącej wartości. |
IBinaryInteger<Int32>.TryWriteBigEndian(Span<Byte>, Int32) |
Próbuje zapisać bieżącą wartość w formacie big-endian do danego zakresu. |
IBinaryInteger<Int32>.TryWriteLittleEndian(Span<Byte>, Int32) |
Próbuje zapisać bieżącą wartość w formacie little-endian do danego zakresu. |
IComparable.CompareTo(Object) |
Porównuje to wystąpienie z określonym obiektem i zwraca wskazanie ich względnych wartości. |
IConvertible.GetTypeCode() | |
IConvertible.ToBoolean(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToBoolean(IFormatProvider). |
IConvertible.ToByte(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToByte(IFormatProvider). |
IConvertible.ToChar(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToChar(IFormatProvider). |
IConvertible.ToDateTime(IFormatProvider) |
Ta konwersja nie jest obsługiwana. Próba użycia tej metody zgłasza błąd InvalidCastException. |
IConvertible.ToDecimal(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToDecimal(IFormatProvider). |
IConvertible.ToDouble(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToDouble(IFormatProvider). |
IConvertible.ToInt16(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToInt16(IFormatProvider). |
IConvertible.ToInt32(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToInt32(IFormatProvider). |
IConvertible.ToInt64(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToInt64(IFormatProvider). |
IConvertible.ToSByte(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToSByte(IFormatProvider). |
IConvertible.ToSingle(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToSingle(IFormatProvider). |
IConvertible.ToType(Type, IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToType(Type, IFormatProvider). |
IConvertible.ToUInt16(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToUInt16(IFormatProvider). |
IConvertible.ToUInt32(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToUInt32(IFormatProvider). |
IConvertible.ToUInt64(IFormatProvider) |
Aby uzyskać opis tego elementu członkowskiego, zobacz ToUInt64(IFormatProvider). |
Dotyczy
Bezpieczeństwo wątkowe
Wszystkie elementy członkowskie tego typu są bezpieczne wątkowo. Elementy członkowskie, które na pierwszy rzut oka modyfikują stan wystąpienia, w rzeczywistości zwracają nowe wystąpienie zainicjowane z nową wartością. Podobnie jak w przypadku innych typów odczytywanie i zapisywanie w udostępnionej zmiennej, która zawiera wystąpienie tego typu, musi być chronione przez blokadę w celu zagwarantowania bezpieczeństwa wątków.