整數溢位
整數可以儲存最大容量。當你超過這個限制時,它將回到負面。對於 int
,它是 2147483647
int x = int.MaxValue; //MaxValue is 2147483647
x = unchecked(x + 1); //make operation explicitly unchecked so that the example also works when the check for arithmetic overflow/underflow is enabled in the project settings
Console.WriteLine(x); //Will print -2147483648
Console.WriteLine(int.MinValue); //Same as Min value
對於此範圍之外的任何整數,使用具有資料型別 BigInteger 的名稱空間 System.Numerics。請檢視以下連結以獲取更多資訊 https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx