C# Int32 MinValue

Description

Int32 MinValue represents the smallest possible value of Int32. This field is constant.

Syntax

Int32.MinValue has the following syntax.


public const int MinValue

Example

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


using System;/*  ww  w.j  ava  2 s.com*/

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