C # 's checked and unchecked operators

Source: Internet
Author: User
Tags constant integer throw exception

The checked and unchecked operators control overflow checking in the current environment when used in integer arithmetic operations. The following operations participate in the checked and uncheced checks:

Predefined + + and--: unary operator, when its operand type is integral type.

Predefined-: unary operator, when its operand is an integer number.

Predefined + 、-、 *,/etc two-element operator, when two operand data types are integral.

Explicit data conversion from one integral type to another.

When one of these operations produces a large number that cannot be represented by a target type, in an expression using the checked operator, if the operation is a constant expression, a run-time error is generated, otherwise the exception is overflowed when the operation is executed. In an expression using the uncheced operator, the return value is truncated to a position that does not conform to the target type.

If the expression is not included by any checked or unchecked operator or statement, the runtime computes the value of the expression, depending on external factors such as compiler state, execution environment parameters, and so on. For a constant expression, it always defaults to overflow checking.

When the unchecked operator is used, the overflow does not cause a compilation error. However, this tends to result in unexpected results, so be careful with the unchecked operator.

The following examples illustrate the use of the checked and unchecked operators:

Class Test
{
  static int x=1000000;
  static int y=1000000;
      Static intf () {return
      checked (x*y);//Compile error, throw exception
  }
      static int G () {return
      unchecked (x*y); The returned value is -727379968
  }
      static int H () {return
      x*y;  dependent on compile-time defaults
  }
}

Compilation errors are not generated because no expressions are evaluated at compile time. When the program runs, the call to the F method throws a OverflowException exception, while the call G method returns-727379968 (the lower 32 bits out of the result range). Whether the H method throws an exception depends on the default of the compilation environment, and the result of calling it may be the same as f, or it may be the same as G. For a constant expression, it is another case:

Class Test
{
  const int x=1000000;
  const int y=1000000;
      static int F () {return
      checked (x*y);//Compile Error, overflow
  }
      static int G () {return
      unchecked (x*y); The return value is -727379968
  }
      static int H () {returns
      x*y;
  }
}

An overflow occurs when the constant expression in the F and H methods is computed, and the error is reported at compile time because it is in a checked environment. When you compute a constant expression in the G method, an overflow occurs, but no errors are reported.

The checked and unchecked operators only affect overflow checks that are placed in the parentheses behind the operation. They do not affect the invoked method members as the result of an expression. For example:

Class Test
{
  static int F (int x,int y) {return
     x*y;
  }
  static int G () {return
  checked (F (1000000,1000000));
  }

Where the checked in the G method does not affect the x*y operation in the function f method, the x*y operation is in the context of the default overflow check.

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.