C # delicious: Use the checked statement to prevent data Overflow

Source: Internet
Author: User
Document directory
  • Checked
  •  
  • Unchecked
  • Several Notes
  • References

In C #, there is a keyword checked, which is used to determine whether the value calculation and Numerical Conversion in the current context will overflow. If it is a constant overflow, it can be found during compilation; if it is a variable overflow, overflowexception will be thrown during runtime.

Numeric operations include: ++--(unary) + -*/

 

With this, you don't have to worry about data overflow.

 

Checked

There are two ways to use checked:

1.Used as an operator

int a = int.MinValue;int c = checked(a--);

An exception is thrown during execution:

2.Check a large piece of code:

This will check all the code in it.

checked{    int a = int.MinValue;    int c = a--;}

For example:

Unchecked

There is an unchecked keyword corresponding to checked to cancel the check.

It is also used in two ways;

1.As an operator:

int a = int.MinValue;int c = unchecked(a--);

In this way, no exception will be thrown.

 

2.Check a large piece of code

unchecked{    int a = int.MinValue;    int c = a--;}

No exception is thrown:

 

/Checked and/checked-


Is it annoying to write so many checked statements in the code? If you have a compilation parameter, check the parameter only when it is set. Microsoft also thought of this. It provides a/checked parameter and A/checked-to cancel it.

  • Overflow check/checked, or/checked +
  • Overflow not checked/checked-

Of course, you can cancel all the checks. The command line parameter is/checked-

csc t2.cs /checked

In this example, csc.exe and t2.cs are compiled code files.

I think many people use Visual Studio. VS is also configurable.

The procedure is as follows. I use vs2010 as an example (vs2005, 2008 is similar)

1. Right-click the project and choose menuProperties

2. Click "build" and then "advanced"

3. In the displayed dialog box, check for Arithmetic overflow/underflow

Several Notes

1.The checked statement is only valid for the code in the current context. It does not check the called function..

static void Main(string[] args){    checked    {        TestFun();    }}static void TestFun(){    int a = int.MinValue;    int c = a--;}

In this Code. No running exception, because the checked keyword does not affect the internal testfun. To do this, either add the checked keyword to testfun or enable the global switch (add the compilation parameter/checked or ).

2.The checked and unchecked keywords do not check whether the Left shift and right shift overflow.

static void Main(string[] args){    checked    {        int a = int.MinValue;        int c = a>>1;    }}

The execution will not throw an exception:

 

3.For the sake of performance, we recommend that you perform check in debug and not in release.

 

References

/Checked (check integer arithmetic)

Http://msdn.microsoft.com/en-us/library/h25wtyxf (V = vs.71). aspx

Arithmetic overflow checking using checked/unchecked

Http://www.codeproject.com/KB/cs/overflow_checking.aspx

C #3.0 in a nutshell, 3rd Edition

Chapter 2.4.5.2. Integral Overflow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.