C# Int32结构用法示例和介绍

2021年5月9日16:22:35 发表评论 730 次浏览

C#中, Int32 Struct表示32位有符号整数(也称为int数据类型)从范围开始-2, 147, 483, 648至+2, 147, 483, 647。它还提供了不同类型的方法来执行各种操作。你可以对Int32类型执行数学运算, 如加, 减, 乘等。它支持AND, OR, XOR等按位运算。它完全支持标准和自定义数字格式字符串。你可以调用以下方法兑换和数学课对Int32值执行运算。 Int32结构继承了ValueType类, 该类继承了对象类别.

领域

领域 描述
MaxValue 该字段用于表示Int32的最大可能值。该字段是恒定的。
MinValue 该字段用于表示Int32的最小可能值。该字段是恒定的。

例子:

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

方法

方法 描述
CompareTo() 此方法用于将此实例与指定的对象或Int32进行比较, 并返回其相对值的指示。
Equals() 此方法用于返回一个值, 该值指示此实例是否等于指定的对象或Int32。
GetHashCode() 此方法用于返回此实例的哈希码。
GetTypeCode() 此方法用于返回值类型Int32的TypeCode。
Parse() 此方法用于将数字的字符串表示形式转换为其等效的32位有符号整数。
ToString() 此方法用于将该实例的数值转换为其等效的字符串表示形式。
TryParse() 此方法用于将数字的字符串表示形式转换为其等效的32位有符号整数。返回值指示操作是否成功。

示例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/en-us/dotnet/api/system.int32?view=netframework-4.7.2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: