C# Int32 MaxValue

Description

Int32 MaxValue represents the largest possible value of an Int32. This field is constant.

Syntax

Int32.MaxValue has the following syntax.


public const int MaxValue

Example

The following example uses the MaxValue property to prevent an OverflowException when converting to an Int32 value.


using System;/* w  w w.j ava 2 s.c o  m*/

public class Class1
{
   public static void Main()
   {
      long[] numbersToConvert = { 123456789, 987654321, -54000, Int64.MaxValue/2 };
      int newNumber;
      foreach (long number in numbersToConvert)
      {
         if (number >= Int32.MinValue && number <= Int32.MaxValue)
         {
            newNumber = Convert.ToInt32(number);
            Console.WriteLine("Successfully converted {0} to an Int32.", 
                              newNumber);
         }
         else
         {
            Console.WriteLine("Unable to convert {0} to an Int32.", number);
         }
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version