Unchecked Operator

Definition - What does Unchecked Operator mean?

An unchecked operator, in C#, is an operator used to disable overflow checking during the evaluation of expressions and conversions.

While performing integral-type arithmetic operations using expressions containing numeric data types, it can result in an overflow exception if the result of the operation is too large to be represented in the number that is allocated with a specific number of bits based on its type. In cases where an overflow is an acceptable result of operation, it is necessary to suppress the exception and continue with the program. An unchecked operator is used in such scenarios by setting an unchecked context in which the arithmetic overflow is ignored with the truncation of the result.

In contrast to C/C++, which ignores the possibility of overflow, C# provides the feature to either handle or ignore overflow by providing checked and unchecked operators. The unchecked operator is often used in the section of a project that is compiled in checked context. Some of the algorithms where the unchecked operator can be used include checksums, encryption logic and random number generation.

Techopedia explains Unchecked Operator

An unchecked operator is used with the keyword "unchecked" on an expression and as a statement in a block of code. It sets an execution context in which the most significant bits of the result are discarded on the occurrence of an integer overflow, while execution is continued. The context of unchecked is applicable to the statements enclosed within the "unchecked" statement and not to nested function calls.

For example, when the product of two large integers of data type requires the use of short for some purpose without any necessity for checking for the occurrence of an overflow, an unchecked operator can be used.

In general, an expression that contains constant terms can only be checked for an overflow during compile time. An unchecked operator can be used to avoid checking for overflow in such expressions and also for expressions that contain non-constant terms that are unchecked by default, both at run time and compile time.

An unchecked operator cannot be overloaded.

Because the use of an unchecked operator has a performance penalty, it should only be used where the overflow does not cause any issues.

This definition was written in the context of C#.
Posted by:

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
Techopedia on Twitter

Sign up for Techopedia's Free Newsletter!
Techwise Webinar Series
Understanding the Big Data Landscape
Understanding the Big Data Landscape
Big Data is everywhere, that's for sure. But the big question for today's savvy enterprise is: exactly where does Big Data fit into your organization?

Email Newsletter

Join 138,000+ IT pros on our weekly newsletter

Free 30 Day Trial: SolarWinds® Log & Event Manager
Free 30 Day Trial: SolarWinds® Log & Event Manager:
Use this powerful SIEM to tackle compliance audits, perform root cause analysis and manage all logs in one place.
Free Whitepaper – Bridging the IT Operations Application Owner Gap
Free Whitepaper – Bridging the IT Operations Application Owner Gap:
Conflicting organizational needs all too often create tension between functional silos. Application owners demand more and more resources.
Free 30 Day Trial – VMTurbo Operations Manager
Free 30 Day Trial – VMTurbo Operations Manager:
VMTurbo's flagship product, Operations Manager maintains your virtual and cloud environments in a healthy state. It manages homogeneous and...