Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

checked (C# Reference)

The checked keyword is used to explicitly enable overflow-checking for integral-type arithmetic operations and conversions.

By default, if an expression produces a value that is outside the range of the destination type, constant expressions cause compile-time errors, and non-constant expressions are evaluated at run-time and raise exceptions. However, the checked keyword can be used to enable checking if it is suppressed globally by compiler options or environment configuration.

See the unchecked examples on using the unchecked keyword.

This sample shows how to use checked for a non-constant expression. The overflow is reported at run-time.

// statements_checked.cs
using System;
class OverFlowTest
    static short x = 32767;   // Max short value
    static short y = 32767;

    // Using a checked expression 
    static int CheckedMethod()
        int z = 0;
            z = checked((short)(x + y));
        catch (System.OverflowException e)
        return z;

    static void Main()
        Console.WriteLine("Checked output value is: {0}", 

Sample Output

System.OverflowException: Arithmetic operation resulted in an overflow.
   at OverFlowTest.CheckedMethod()
Checked output value is: 0

For more information, see the following sections in the C# Language Specification:

  • Block statements, checked, and unchecked statements

  • 7.5.12 The checked and unchecked operators

  • 8.11 The checked and unchecked statements

© 2015 Microsoft