Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0060 Integral type overflow

The integral type may overflow. The following example shows that the overflow happens when subtracting one from minimum int value. using System; class Program { static void Main(string[] args) { int i = int.MinValue; int result = i-1; Console.WriteLine("i=" + i); Console.WriteLine("result="+result); Console.WriteLine("result is int.MaxValue:" + (result == int.MaxValue)); } } The output: i=-2147483648 result=2147483647 result is int.MaxValue:True