
C# 7 and .NET Core: Modern Cross-Platform Development

Earlier, we saw that when casting between number types, it was possible to lose information, for example, when casting from a long
variable to an int
variable. If the value stored in a type is too big, it will overflow.
Add a new console application project named Ch03_CheckingForOverflow
.
The checked
statement tells .NET to throw an exception when an overflow happens instead of allowing it to happen silently.
We set the initial value of an int
variable to its maximum value minus one. Then, we increment it several times, outputting its value each time. Note that once it gets above its maximum value, it overflows to its minimum value and continues incrementing from there.
Type the following code in the Main
method and run the program:
int x = int.MaxValue - 1; WriteLine(x); x++; WriteLine(x); x++; WriteLine(x); x++; WriteLine(x);
Run the console application and view the output...
Change the font size
Change margin width
Change background colour