Int32.MaxValue 字段在 C# 中使用示例


Int32.MaxValue 字段在 C# 中用来表示 Int32 的最大可能值。

语法

语法如下 −

public const int MaxValue = 2147483647;

示例

现在,让我们看一个示例来实现 Int32.MaxValue 字段 −

using System;
public class Demo {
   public static void Main(){
      int val1 = 23;
      int val2 = 23;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      Console.WriteLine("TypeCode for val1 = "+type1);
      Console.WriteLine("TypeCode for val2 = "+type2);
      Console.WriteLine("Maximum Value = "+ Int32.MaxValue);
   }
}

输出

这将产生以下输出 −

Value1 = 23
Value2 = 23
HashCode for value1 = 23
HashCode for value2 = 23
Are they equal? = True
TypeCode for val1 = Int32
TypeCode for val2 = Int32
Maximum Value = 2147483647

示例

现在,让我们看另一个示例来实现 Int32.MaxValue 字段 −

using System;
public class Demo {
   public static void Main(){
      long[]val = {8998, -899898778, 8787867687878, -20};
      int res;
      foreach(long a in val){
         if(a >= Int32.MinValue && a <= Int32.MaxValue){
            res = Convert.ToInt32(a);
            Console.WriteLine("Converted: "+res);
         }
         else{
            Console.WriteLine("Not Possible");
         }
      }
   }
}

输出

这将产生以下输出 −

Converted: 8998
Converted: -899898778
Not Possible
Converted: -20

更新于: 13-Nov-2019

386 次浏览

开启您的 职业生涯

完成课程并获得认证

开始
广告