Fun Sum

Watch out when summing up numbers as bugs can sneak up on you without a warning. Check these two approaches out:

int maxInt = int.MaxValue;
Console.WriteLine(maxInt + 1); // Prints out -2147483648
Console.WriteLine(new List<int> { maxInt, 1 }.Sum()); // Throws System.OverflowException

A guest on .NET Rocks, “Testing Software using PEX“, show¬†asked which approach throws an exception. Now, we know…

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.