C# Int16结构用法示例介绍

2021年5月9日16:24:18 发表评论 794 次浏览

C#中, Int16结构表示16位带符号整数(也称为short数据类型)从范围开始-32768至+32767。它提供了不同类型的方法来执行各种操作, 例如将这种类型的实例的值转换为其字符串表示形式, 将数字的字符串表示形式转换为该类型的实例以及比较该类型的实例等。你还可以调用math类的方法来执行数学运算。该结构定义在系统命名空间。 Int16结构继承了ValueType类, 该类继承了对象类别.

领域

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

例子:

//C# program to illustrate the
//MaxValue and MinValue field
using System;
  
class GFG {
  
     //Main method
     static public void Main()
     {
         Int16 var1 = 100;
         Int16 var2 = 30;
         Int16 var3 = 50;
  
         //Get the Maximum and Minimum value of 
         //Int16 type Using MaxValue and the 
         //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}" , Int16.MaxValue);
         Console.WriteLine( "Minimum Value: {0}" , Int16.MinValue);
     }
}

输出如下:

Value 1: 100
Value 2: 30
Value 3: 50
Maximum Value: 32767
Minimum Value: -32768

方法

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

示例1:

//C# program to demonstrate the 
//Int16.Equals(Object) Method 
using System; 
    
class GFG { 
    
     //Main Method 
     public static void Main() 
     { 
         //Declaring and initializing value1 
         short value1 = 57; 
    
         //Declaring and initializing value2 
         //It will convert into Int16 implicitly  
         //by the compiler to check whether it is  
         //in the range of short data type i.e.  
         //Int16 or not 
         object value2 = 47; 
    
         //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); 
     } 
}

输出如下:

57 is not equal to 47

示例2:

//C# program to illustrate the 
//GetHashCode() method
using System;
  
class GFG {
  
     //Main method
     static public void Main()
     {
         Int16 var1 = 100;
         Int16 var2 = 30;
         Int16 var3 = 50;
  
         //Get the hash code of all the variables
         //Using GetHashCode() method
         Console.WriteLine( "Get the hash code of var1: {0}" , var1.GetHashCode());
  
         Console.WriteLine( "Get the hash code of var2: {0}" , var2.GetHashCode());
  
         Console.WriteLine( "Get the hash code of var3: {0}" , var3.GetHashCode());
     }
}

输出如下:

Get the hash code of var1: 6553700
Get the hash code of var2: 1966110
Get the hash code of var3: 3276850

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.int16?view=netframework-4.7.2

木子山

发表评论

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