|
Monday, 09 March 2009
|
|
HTML clipboard
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
{
int
total = 2147483647 * 2;
return
total;
}
}
|