当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Int16.MaxValue用法及代码示例


Int16 Struct的MaxValue字段或属性用于表示Int16的最大值。该字段的值是常量,表示用户无法更改该字段的值。该字段的值为32767。其十六进制值为0x7FFF。从较大范围的数字类型(例如UInt16或Int32)转换为Int16时,它用于避免OverflowException。

用法:

public const short MaxValue = 32767;

返回值:该字段始终返回32767。


例:

// C# program to illustrate the 
// Int16.MaxValue field 
using System; 
   
class GFG { 
   
    // Main Method 
    static public void Main() 
    { 
        // display the Maximum value  
        // of Int16 struct 
        Console.WriteLine("Maximum Value is:"+ 
                                Int16.MaxValue); 
   
        // Taking an array of long i.e Int64 data type 
        long[]num = {345, -4677, -65245465445441, 44456564}; 
   
        // taking variable of Int16 type 
        short mynum; 
        foreach(long n in num){ 
   
            if(n >= Int16.MinValue && n <= Int16.MaxValue) 
            { 
   
                // using the method of Convert class 
                // to convert Int64 to Int16  
                mynum = Convert.ToInt16(n); 
                Console.WriteLine("Conversion is Possible."); 
            } 
            else
            { 
                Console.WriteLine("Not Possible"); 
            } 
        } 
           
    } 
}

输出:

Maximum Value is:32767
Conversion is Possible.
Conversion is Possible.
Not Possible
Not Possible

参考:



相关用法


注:本文由纯净天空筛选整理自ankita_saini大神的英文原创作品 Int16.MaxValue Field in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。