C # basic syntax Learning (10 ),

Source: Internet
Author: User

C # basic syntax Learning (10 ),

C # overflow may occur when data is displayed and converted. In this case, you can use the keyword checked to check for overflow:

Checked (<expression>) checks for Overflow
Unchecked (<expression>) does not check Overflow

If you use checked to check for overflow, System will be thrown once the overflow occurs. overflowException. You can also configure the IDE to enable the overflow check by default. Unless unchecked is added, an exception will be thrown once overflow occurs. The default overflow check for configuration projects is as follows:

An example of using checked is as follows:

1 static void Main (string [] args) 2 {3 Int32 a = 123456; 4 Int16 B = 0; 5 6 B = checked (Int16) a); 7 8 Console. writeLine ("a = {0} \ r \ nb = {1}", a, B); 9 10 Console. writeLine ("Press any key to exit! "); 11 Console. ReadKey (); 12}

Running result:

Unhandled Exception: System. overflowException: Arithmetic operation resulted inan overflow. at CheckOverflow. program. main (String [] args) in d: \ Nick \ code \ C # \ test \ CheckOverflow \ Program. cs: line 16

An exception is thrown during conversion 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.