Adding 2 integers (32-bit)
A 32-bit signed integer has valid values ranging from−2,147,483,648 to +2,147,483,647.
So why do I always see sample code of an integer addition method written in C# like this:
private static int add(int a, int b)
return a + b;
}
So if I were to add 2,000,000,000 and 2,000,000,000 - an integer datatype would overflow. But the solution isn’t merely to set the return value to a “long” (i.e. 64-bit integer):
private static long add(int a, int b)
return a + b;
}
You have to cast each parameter variable to a long before performing the addition:
private static long add(int a, int b)
return (long)a + (long)b;
}
Here are some I found:
http://www.cs.tufts.edu/comp/194NET/notes/csharp.php3
http://www.harding.edu/fmccown/java1_5_csharp_comparison.html
http://www.developernotes.com/post/Use-Ruby-to-Unit-Test-C.aspx