Book Image

C# 7 and .NET: Designing Modern Cross-platform Applications

By : Mark J. Price, Ovais Mehboob Ahmed Khan
Book Image

C# 7 and .NET: Designing Modern Cross-platform Applications

By: Mark J. Price, Ovais Mehboob Ahmed Khan

Overview of this book

C# is a widely used programming language, thanks to its easy learning curve, versatility, and support for modern paradigms. The language is used to create desktop apps, background services, web apps, and mobile apps. .NET Core is open source and compatible with Mac OS and Linux. There is no limit to what you can achieve with C# and .NET Core. This Learning Path begins with the basics of C# and object-oriented programming (OOP) and explores features of C#, such as tuples, pattern matching, and out variables. You will understand.NET Standard 2.0 class libraries and ASP.NET Core 2.0, and create professional websites, services, and applications. You will become familiar with mobile app development using Xamarin.Forms and learn to develop high-performing applications by writing optimized code with various profiling techniques. By the end of C# 7 and .NET: Designing Modern Cross-platform Applications, you will have all the knowledge required to build modern, cross-platform apps using C# and .NET. This Learning Path includes content from the following Packt products: • C# 7.1 and .NET Core 2.0 - Modern Cross-Platform Development - Third Edition by Mark J. Price • C# 7 and .NET Core 2.0 High Performance by Ovais Mehboob Ahmed Khan
Table of Contents (25 chapters)
Title Page
Copyright
About Packt
Contributors
Preface
16
Designing Guidelines for .NET Core Application Performance
Index

Checking for overflow


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 CheckingForOverflow.

The checked statement

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:

2147483646
2147483647
-2147483648
-2147483647

Now, let's get the compiler to warn us about the overflow using the checked statement:

checked
{ 
   int x = int.MaxValue - 1; 
   WriteLine(x); 
   x++; 
   WriteLine(x); 
   x++; 
   WriteLine(x); 
   x++; 
   WriteLine(x); 
}

Run the console application and view the output:

2147483646
2147483647
Unhandled Exception: System.OverflowException: Arithmetic operation
resulted in an overflow.

Just like any other exception, we should wrap these statements in a try block and display a nicer error message for the user:

try
{
   // previous code goes here
}
catch(OverflowException)
{
   WriteLine("The code overflowed but I caught the exception.");
}

Run the console application and view the output:

2147483646
2147483647
The code overflowed but I caught the exception.

The unchecked statement

A related keyword is unchecked. This keyword switches off overflow checks within a block of code.

Type the following statement at the end of the previous statements. The compiler will not compile this statement because it knows it would overflow:

int y = int.MaxValue + 1; 

Press F6 or enter the dotnet runcommandto build and notice the error, as shown in the following screenshot from Visual Studio 2017:

Note that this is a compile-time check. To disable compile-time checks, we can wrap the statement in an unchecked block, as shown in the following code:

unchecked
{ 
   int y = int.MaxValue + 1; 
   WriteLine(y); // this will output -2147483648  
   y--;  
   WriteLine(y); // this will output 2147483647  
   y--;  
   WriteLine(y); // this will output 2147483646 
} 

Run the console application and view the output:

2147483646
2147483647
The code overflowed but I caught the exception.
-2147483648
2147483647
2147483646

Of course, it would be rare that you would want to explicitly switch off a check like this because it allows an overflow to occur. But, perhaps, you can think of a scenario where you might want that behavior.