📜  C#| Int16结构

📅  最后修改于: 2021-05-29 16:33:37             🧑  作者: Mango

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

领域

Fields Description
MaxValue This field is used to represent the largest possible value of an Int16. This field is constant.
MinValue This field is used to represent the smallest possible value of Int16. This field is constant.

例子:

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

方法

Method Description
CompareTo() This method is used to compare this instance to a specified object or another Int16 instance and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object or the other Int16 instance.
Equals() This method is used to return a value indicating whether this instance is equal to a specified object or Int16.
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 Int16.
Parse() This method is used to convert the string representation of a number to its 16-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 16-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

范例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/zh-cn/dotnet/api/system.int16?view=netframework-4.7.2