If you wish to force overflow checking to occur over

This preview shows page 30 - 33 out of 37 pages.

If you wish to force overflow checking to occur over a block of code statements, you can do so by defining a checked scope as follows: try { checked { byte sum = (byte)Add(b1, b2); Console.WriteLine("sum = {0}", sum); } } catch (OverflowException ex) { Console.WriteLine(ex.Message); } CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I 98
In either case, the code in question will be evaluated for possible overflow conditions automat- ically, which will trigger an overflow exception if encountered. Setting Projectwide Overflow Checking If you are creating an application that should never allow silent overflow to occur, you may find yourself in the annoying position of wrapping numerous lines of code within the scope of the checked keyword. As an alternative, the C# compiler supports the /checked flag. When enabled, all of your arithmetic will be evaluated for overflow without the need to make use of the C# checked keyword. If overflow has been discovered, you will still receive a runtime exception. To enable this flag using Visual Studio 2008, open your project’s property page and click the Advanced button on the Build tab. From the resulting dialog box, select the Check for arithmetic overflow/underflow check box (see Figure 3-17). Figure 3-17. Enabling projectwide overflow/underflow data checking Enabling this setting can be very helpful when you’re creating a debug build. Once all of the overflow exceptions have been squashed out of the code base, you’re free to disable the /checked flag for subsequent builds (which will increase the runtime performance of your application). The unchecked Keyword Now, assuming you have enabled this projectwide setting, what are you to do if you have a block of code where data loss is acceptable? Given that the /checked flag will evaluate all arithmetic logic, C# provides the unchecked keyword to disable the throwing of an overflow exception on a case-by-case basis. This keyword’s use is identical to that of the checked keyword in that you can specify a single statement or a block of statements, for example: // Assuming /checked is enabled, // this block will not trigger // a runtime exception. unchecked { byte sum = (byte)(b1 + b2); Console.WriteLine("sum = { 0} ", sum); } CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I 99
So, to summarize the C# checked and unchecked keywords, remember that the default behavior of the .NET runtime is to ignore arithmetic overflow. When you want to selectively handle discrete statements, make use of the checked keyword. If you wish to trap overflow errors throughout your application, enable the /checked flag. Finally, the unchecked keyword may be used if you have a block of code where overflow is acceptable (and thus should not trigger a runtime exception). The Role of System.Convert To wrap up the topic of data type conversions, I’d like to point out the fact that the System name- space defines a class named Convert that can also be used to widen or narrow data: static void NarrowWithConvert() { byte myByte = 0; int myInt = 200; myByte = Convert.ToByte (myInt); Console.WriteLine("Value of myByte: {0}", myByte); } One benefit of using

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture