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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。