C # uses checked and unchecked

Source: Internet
Author: User

The first thing to know is that the int type is a 32-bit number in C #. This allows you to know the range of values for int is ( -2147483648~2147483647) when you want to use the minimum or maximum value of int, you can use Int. Both the MinValue and Int.maxvalue properties.

Here we are going to talk about an int number overflow, in fact, when the maximum of +1 in the int type becomes the minimum value. When we want this int value to not exceed the limit, you can manually enable the overflow check feature.

In Visual Studio, you are allowed to set project properties to enable or disable overflow checking. Solution Explorer------Right-click to select Properties-------in the Properties dialog box, tick generate---------Click the Advanced button in the lower right corner--------the check operation overflow/underflow option in the Advanced Build Settings dialog box

You can use the checked and unchecked keywords in your code to selectively open and close integer overflow checking for a specific part of a program that overrides the project's compiler options.

The checked keyword is open arithmetic overflow check, unchecked opposite.

  

The above program sets the A variable to the maximum value of the int data, and throws a OverflowException exception when the checked detection is performed.

The use of unchecked forces a block of code that does not check for overflow. This exception is not thrown OverflowException.

It is important to note that only integer operations in checked fast are checked, and for fast method calls, the integer operation in the called method is not checked.

Exception handlers and the Visual Studio debugger

In Visual Studio, the debugger will break the application by default only when an unhandled exception occurs, but sometimes you need to debug the application itself, which requires that the agenda be tracked before the application catches it. It is easy to enable this feature:

Select-----Debug Window-----Exception settings

C # uses checked and unchecked

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.