CSharp checked and unchecked
When you create a project in Visual Studio, by default, numbers can be added, multiplied, divided or subtracted in such a way as to create a number too big for the variable that is holding the number.
You can change this at the project level for a Windows exe project by going to project properties, selecting the build tab, clicking the “Advanced” tab and checking the checkbox, “Check for arithmetic overflow/underflow.”
In a web application, you can add a compilerOptions attribute to the compiler element and add the /checked flag.
<compilation debug="true" compilerOptions="/checked"/> <assemblies> <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> </assemblies> </compilation>
You can also override whatever you set for the default in your compiler options by using the two keywords checked and unchecked in your code.
So, maybe you’ve decided that all of your code should check for overflows and underflows but there is one section of code that you want to allow to overflow or underflow. You would use unchecked:
int a = int.MaxValue; int b = int.MaxValue; unchecked { a = a + b; }
On the other hand, if you want to make unchecked the default, you can override that by using the keyword checked:
int a = int.MaxValue; int b = int.MaxValue; try { a = checked(a + b); } catch (OverflowException ofe) { // appropriate handler here }
Note that if you use checked or that is your default, overflows and underflows will throw an exception, so we’ve added a try/catch block to our code.
Other post in Advanced CSharp
- Two Interfaces. Same Method. Two meanings. - September 29th, 2008
- Making values nullable - October 9th, 2008
- CSharp's Property Shortcuts - October 23rd, 2008
- Readonly variables in CSharp? Really?! - October 29th, 2008
- Dispose with Using - November 10th, 2008
- Delegates in .NET - December 4th, 2008
- Using Sealed in CSharp - December 8th, 2008
- CSharp checked and unchecked - December 11th, 2008
- Advanced CSharp - unsafe mode - December 15th, 2008
- Volatile variables and CSharp threads - December 22nd, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- CSharp fixed keyword - January 5th, 2009
- using - There's more there than you are using - February 2nd, 2009
- Stackalloc in CSharp - February 16th, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- && vs & and | vs ||... What's the difference? - March 16th, 2009
- Advanced CSharp - yield - March 25th, 2009
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- CSharp ?? Operator - May 18th, 2009
- Using VB.NET From CSharp - July 1st, 2009
- Dispose, Finalize and SuppressFinalize - July 9th, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
Other Related Items:
Cotton Checked Dress 2009 Adora doll outfitHere is a real cute 20" 2009 Adora doll outfit! This Cotton Checked Dress is the same high quality you are used to with all of the Adora clothes. ... Read More >
All Purpose NetThey are specifically designed for koi, surface skimming, and general all-purpose use. Heavy duty aluminum construction, telescoping handle to 6-1/2 . Soft fish safe nylon netting. Sure-grip handle. Aluminum & Nylon Mesh.










I didn’t know that these statements exist until I ran into your blog. Thanks!