unchecked keyword
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
unchecked keyword
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  A Simple Guide to .Net Remoting [next Lesson]  enum keyword
unchecked keyword
  by: Salman Ahmed

unchecked

The unchecked keyword is designed to control the overflow-checking for integral-type operations and any conversions you may be doing. You can use unchecked as either a operator or a statement.

Statement:

unchecked block
Operator:

unchecked (expression)
Where:
  • block - the entire block that contains the expressions is to be evaluated in an unchecked context
  • expression - a expression (in parentheses) is to be evaluated in a unchecked context only
Key Points:

The default overflow checking is checked (i.e you can either explicitly write checked or do nothing and it will implicitly be treated as checked), which means you will receive a "The operation overflows at compile time in checked mode". If on the other hand you specifiy unchecked in either the statement block or expression then the expression will be truncated (if it falls outside the range of the destination type).

Example:


public void Page_Load(object sender, EventArgs e)
{
   Response.Write(UncheckedTest());
}
public int UncheckedTest()
{
    // Unchecked used as a statement
    // Without this statement you will get the following error:
    // CS0220: The operation overflows at compile time in checked mode
    unchecked
    {
         // max int value multiplied by 2
         // which usually overflows
         int total = 2147483647 * 2;   
         return total;
    }
}

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
unchecked keyword
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  A Simple Guide to .Net Remoting [next Lesson]  enum keyword


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
snaso 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center