Information Technology Reference
In-Depth Information
Overflow Checking Context
You've seen that explicit conversions have the possibility of losing data and not being able to
represent the source value equivalently in the target type. C# provides you with the ability to
choose whether the runtime should check the result for overflow when making these types of
conversions. It does this through the
checked
operator and statement.
Whether a segment of code is checked or not is called its
overflow checking context
.
-
If you designate an expression or segment of code as
checked
, the CLR will raise an
OverflowException
exception if the conversion produces an overflow.
If the code is not
checked
, the conversion will proceed regardless of whether there is
an overflow.
-
The default overflow checking context is
not
checked
.
The checked and unchecked Operators
The
checked
and
unchecked
operators control the overflow checking context of an expression,
which is placed between a set of parentheses. The syntax is the following:
checked (
Expression
)
unchecked (
Expression
)
For example, the following code executes the same conversion—first in a
checked
operator
and then in an
unchecked
operator.
In the
unchecked
context, the overflow is ignored, resulting in the value
208
.
In the
checked
context, an
OverflowException
exception is raised.
ushort sh = 2000;
byte sb;
sb = unchecked ( (byte) sh ); // Most significant bits lost
Console.WriteLine("sb: {0}", sb);
sb = checked ( (byte) sh ); // OverflowException raised
Console.WriteLine("sb: {0}", sb);
![](Illustrated-C-2005_images/img-gen32550.png)
![](Illustrated-C-2005_images/img-gen32551.png)
![](Illustrated-C-2005_images/img-gen32552.png)
![](Illustrated-C-2005_images/img-gen32553.png)
![](Illustrated-C-2005_images/img-gen32554.png)
![](Illustrated-C-2005_images/img-gen32555.png)
![](Illustrated-C-2005_images/img-gen32556.png)
![](Illustrated-C-2005_images/img-gen32557.png)
![](Illustrated-C-2005_images/img-gen32558.png)
![](Illustrated-C-2005_images/img-gen32559.png)
![](Illustrated-C-2005_images/img-gen32560.png)
![](Illustrated-C-2005_images/img-gen32561.png)
![](Illustrated-C-2005_images/img-gen32562.png)
![](Illustrated-C-2005_images/img-gen32563.png)
![](Illustrated-C-2005_images/img-gen32564.png)
![](Illustrated-C-2005_images/img-gen32565.png)
![](Illustrated-C-2005_images/img-gen32566.png)
![](Illustrated-C-2005_images/img-gen32567.png)
![](Illustrated-C-2005_images/img-gen32568.png)
![](Illustrated-C-2005_images/img-gen32569.png)
![](Illustrated-C-2005_images/img-gen32570.png)
![](Illustrated-C-2005_images/img-gen32571.png)
![](Illustrated-C-2005_images/img-gen32572.png)
![](Illustrated-C-2005_images/img-gen32573.png)
![](Illustrated-C-2005_images/img-gen32574.png)
![](Illustrated-C-2005_images/img-gen32575.png)
![](Illustrated-C-2005_images/img-gen32576.png)
![](Illustrated-C-2005_images/img-gen32577.png)
![](Illustrated-C-2005_images/img-gen32578.png)
![](Illustrated-C-2005_images/img-gen32579.png)
![](Illustrated-C-2005_images/img-gen32580.png)
![](Illustrated-C-2005_images/img-gen32581.png)
![](Illustrated-C-2005_images/img-gen32582.png)
![](Illustrated-C-2005_images/img-gen32583.png)
![](Illustrated-C-2005_images/img-gen32584.png)
![](Illustrated-C-2005_images/img-gen32585.png)
![](Illustrated-C-2005_images/img-gen32586.png)
![](Illustrated-C-2005_images/img-gen32587.png)
![](Illustrated-C-2005_images/img-gen32588.png)
![](Illustrated-C-2005_images/img-gen32589.png)
![](Illustrated-C-2005_images/img-gen32590.png)
![](Illustrated-C-2005_images/img-gen32591.png)
![](Illustrated-C-2005_images/img-gen32592.png)
![](Illustrated-C-2005_images/img-gen32593.png)
![](Illustrated-C-2005_images/img-gen32594.png)
![](Illustrated-C-2005_images/img-gen32595.png)
![](Illustrated-C-2005_images/img-gen32596.png)
![](Illustrated-C-2005_images/img-gen32597.png)
![](Illustrated-C-2005_images/img-gen32598.png)
![](Illustrated-C-2005_images/img-gen32599.png)
![](Illustrated-C-2005_images/img-gen32600.png)
![](Illustrated-C-2005_images/img-gen32601.png)
![](Illustrated-C-2005_images/img-gen32602.png)
![](Illustrated-C-2005_images/img-gen32603.png)
![](Illustrated-C-2005_images/img-gen32604.png)
![](Illustrated-C-2005_images/img-gen32605.png)
![](Illustrated-C-2005_images/img-gen32606.png)
![](Illustrated-C-2005_images/img-gen32607.png)
![](Illustrated-C-2005_images/img-gen32608.png)
![](Illustrated-C-2005_images/img-gen32609.png)
![](Illustrated-C-2005_images/img-gen32610.png)
![](Illustrated-C-2005_images/img-gen32611.png)
![](Illustrated-C-2005_images/img-gen32612.png)
![](Illustrated-C-2005_images/img-gen32613.png)
![](Illustrated-C-2005_images/img-gen32614.png)
![](Illustrated-C-2005_images/img-gen32615.png)
![](Illustrated-C-2005_images/img-gen32616.png)
![](Illustrated-C-2005_images/img-gen32617.png)