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


C# Int32.MaxValue用法及代碼示例


Int32 Struct的MaxValue字段或屬性用於表示Int32的最大值。該字段的值是常量,表示用戶無法更改該字段的值。該字段的值為2147483647。其十六進製值為0x7FFFFFFF。它用於避免在轉換為Int32值時發生OverflowException。

用法:

public const int MaxValue = 2147483647;

返回值:該字段始終返回2147483647。


例:

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

輸出:

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

參考:



相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 Int32.MaxValue Field in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。