/checked (Check Integer Arithmetic) (C# Compiler Options)

The /checked option specifies whether an integer arithmetic statement that is not in the scope of the checked or unchecked keywords and that results in a value outside the range of the data type will cause a run-time exception.

/checked[+ | <U>-</U>]

Remarks

An integer arithmetic statement that is in the scope of the checked or unchecked keywords is not subject to the effect of the /checked option.

If an integer arithmetic statement that is not in the scope of the checked or unchecked keywords results in a value outside the range of the data type, and /checked+ (/checked) is used in the compilation, then that statement will cause an exception at run time. If /checked- is used in the compilation, then that statement will not cause an exception at run time.

The default value for this option is /checked-, so you can get the same effect by simply omitting the option altogether. One scenario for using /checked- is in building large applications: sometimes automated tools are used to build such applications, and the tools might automatically set /checked to +: in this situation, you can override the global default by specifying /checked-.

To set this compiler option in the Visual Studio development environment

  1. Open the project's Properties page. For more information, see How to: Set Build Properties (C#).

  2. Click the Build property page.

  3. Click the Advanced button.

  4. Modify the Check for arithmetic overflow/underflow property.

To access this compiler option programmatically, see CheckForOverflowUnderflow.

Example

Compile t2.cs and specify that an integer arithmetic statement that is not in the scope of the checked or unchecked keywords and results in a value outside the range of the data type will cause an exception at run time.

csc t2.cs /checked

See Also

Other Resources

C# Compiler Options

Project Properties (Visual Studio)