在C#中,Int32 Struct表示从-2,147,483,648到+2,147,483,647范围的32位有符号整数(也称为int数据类型)。它还提供了不同类型的方法来执行各种操作。您可以对Int32类型执行数学运算,例如加法,减法,乘法等。它支持AND,OR,XOR等按位运算。它完全支持标准和自定义数字格式字符串。您可以调用Convert和Math类的方法来对Int32值执行操作。 Int32结构继承了ValueType类,而ValueType类继承了Object类。
领域
Fields | Description |
---|---|
MaxValue | This field is used to represent the largest possible value of an Int32. This field is constant. |
MinValue | This field is used to represent the smallest possible value of Int32. This field is constant. |
例子:
// C# program to illustrate the
// MaxValue and MinValue field
using System;
class GFG {
// Main method
static public void Main()
{
Int32 var1 = 34;
Int32 var2 = 30;
Int32 var3 = 59;
// Get the Maximum and Minimum value
// of Int32 type Using MaxValue and
// MinValue field
Console.WriteLine("Value 1: {0}", var1);
Console.WriteLine("Value 2: {0}", var2);
Console.WriteLine("Value 3: {0}", var3);
Console.WriteLine("Maximum Value: {0}",
Int32.MaxValue);
Console.WriteLine("Minimum Value: {0}",
Int32.MinValue);
}
}
输出:
Value 1: 34
Value 2: 30
Value 3: 59
Maximum Value: 2147483647
Minimum Value: -2147483648
方法
Method | Description |
---|---|
CompareTo() | This method is used to compare this instance to a specified object or Int32 and returns an indication of their relative values. |
Equals() | This method is used to return a value indicating whether this instance is equal to a specified object or Int32. |
GetHashCode() | This method is used to return the hash code for this instance. |
GetTypeCode() | This method is used to return the TypeCode for value type Int32. |
Parse() | This method is used to convert the string representation of a number to its 32-bit signed integer equivalent. |
ToString() | This method is used to convert the numeric value of this instance to its equivalent string representation. |
TryParse() | This method is used to convert the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded. |
范例1:
// C# program to demonstrate the
// Int32.Equals(Object) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing value1
int value1 = 70;
// Declaring and initializing value2
object value2 = 89;
// using Equals(object) method
bool status = value1.Equals(value2);
// checking the status
if (status)
Console.WriteLine("{0} is equal to {1}",
value1, value2);
else
Console.WriteLine("{0} is not equal to {1}",
value1, value2);
}
}
输出:
70 is not equal to 89
范例2:
// C# program to illustrate the
// GetTypeCode() method
using System;
class GFG {
// Main method
static public void Main()
{
Int32 var1 = 100;
Int32 var2 = 403;
Int32 var3 = 503;
// Get the type code of
// all the variables
// Using GetTypeCode() method
Console.WriteLine("Get the type code of var1: {0}",
var1.GetTypeCode());
Console.WriteLine("Get the type code of var2: {0}",
var2.GetTypeCode());
Console.WriteLine("Get the type code of var3: {0}",
var3.GetTypeCode());
}
}
输出:
Get the type code of var1: Int32
Get the type code of var2: Int32
Get the type code of var3: Int32
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.int32?view=netframework-4.7.2