Home » Advanced CSharp » CSharp checked and unchecked

CSharp checked and unchecked

coordonnées GPS:   4°315´S     55°719°E 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

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • CSharp Numeric OverflowsCSharp Numeric Overflows Did you know that when you are dealing with numbers, by default, .NET will do, or try to do exactly what you tell it to do?  If you tell it to do the impossible, it will do the next […]
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it […]
  • Authentication – Assigning Permissions to RolesAuthentication – Assigning Permissions to Roles Now that we’ve assigned roles to our users, we need to assign permissions to the roles. The easiest way to do this is through the web.config file, which will allow us to control which […]
  • Unable to find connection…for object web.configUnable to find connection…for object web.config I've seen this error a couple of different times.  Mostly from other people calling me with the problem.  So I still don't know what ultimately causes the problem.  But if you're having […]
  • Forms Authentication – Creating UsersForms Authentication – Creating Users Last week we installed the tables into our database and set up the database connection so that we could implement forms based authentication. This week, we need to put into place a way […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • http://developercontainer.blogspot.com ramil

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