當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。