📜  C#中的Int32.MaxValue字段(带示例)

📅  最后修改于: 2021-05-29 17:35:46             🧑  作者: Mango

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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.int32.maxvalue?view=netframework-4.7.2