Checked And Unchecked Keywords In C# Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    8 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of Checked and Unchecked Keywords in C#

Checked and Unchecked Keywords in C#

Overview

In C#, the checked and unchecked keywords are used to control the context in which overflow checks are performed on arithmetic operations involving integral types (sbyte, byte, short, ushort, int, uint, long, and ulong). Integral type overflows can lead to incorrect results if not handled properly, and these keywords allow developers to manage this risk.

Checked Keyword

  • Purpose: The checked keyword is used to enable overflow checking at compile-time or run-time for arithmetic operations or explicit conversions that involve signed integral types.

  • Usage: You can use checked in three contexts:

    1. Expression Level:

      int a = 2147483647;
      int b = checked(a + 1); // Compiler error: overflow at compile-time
      
    2. Block Level:

      checked {
          int a = 2147483647;
          int b = a + 1; // OverflowCheckException thrown at runtime if executed
      }
      
    3. Contextual Default:

      • By default, constant arithmetic expressions in integer literals are checked.
      • If an overflow occurs during constant expression evaluation, a compiler error results.
  • Compile-time vs. Run-time Checking:

    • Compile-time: This happens when the operation involves only constants (literals or constants defined using const). The compiler will immediately flag an overflow error.
    • Run-time: This happens when the operation involves variables or non-constant values. At runtime, if an overflow occurs, the program will throw a System.OverflowException.

Unchecked Keyword

  • Purpose: The unchecked keyword explicitly suppresses overflow checking for arithmetic operations or conversions that involve signed integral types.

  • Usage: Similar to checked, it can be used at the expression level or block level.

    1. Expression Level:

      int a = 2147483647;
      int b = unchecked(a + 1); // No exception, b becomes -2147483648 due to overflow
      
    2. Block Level:

      unchecked {
          int a = 2147483647;
          int b = a + 1; // No exception, b becomes -2147483648 due to overflow
      }
      
  • Contextual Default:

    • Outside of checked blocks, most code runs in an unchecked context. Therefore, overflow errors in such contexts are not caught unless explicitly specified by wrapping the code in a checked context.
    • The primary use case for unchecked is performance optimization where the programmer is certain no overflow will occur or is willing to handle the consequences.

Important Information

  • Performance Consideration:

    • Enabling overflow checking (using checked) introduces additional code that checks for overflows, potentially decreasing performance.
    • Disabling overflow checking (using unchecked) removes these checks, which can improve performance, especially in loops or performance-critical sections of code.
  • Compile-Time Behavior:

    • Compile-time checking with checked expressions helps catch potential issues early in the development process.
    • This behavior ensures that logical errors resulting from overflows cannot reach execution time.
  • Run-Time Behavior:

    • Run-time checking with checked blocks provides runtime protection against overflows.
    • However, it may lead to runtime exceptions (OverflowException) if an overflow occurs during execution.
  • Default Context:

    • Most of the arithmetic operations and conversions in C# are performed in an unchecked context by default.
    • This is why you typically don’t encounter OverflowException unless there’s an explicit checked block or operation.
  • Compiler Options:

    • The /checked+ and /checked- compiler options can be used to specify a default checking context:
      • /checked+ forces all arithmetic operations to run in a checked context.
      • /checked- ensures all arithmetic operations run in an unchecked context (similar to the default).
  • Practical Applications:

    • Use checked when developing code that needs high precision and cannot tolerate errors resulting from unexpected overflows.
    • Use unchecked when performance optimization is critical, and you have ensured that your operations do not exceed the valid range of the data types.
  • Best Practices:

    • Identify code sections where arithmetic operations could result in overflows due to large input values.
    • Decide whether the risk of an overflow is acceptable or if it should be prevented by using checked.
    • In production code, consider using checked blocks to catch unexpected overflows at run-time, as it’s better to fail fast than to continue with erroneous calculations.

Example Scenario

int a = 2147483647;

try {
    // Overflow detection at run-time
    checked {
        int b = a + 10; 
        Console.WriteLine("Value after addition: " + b);
    }
}
catch (OverflowException ex) {
    Console.WriteLine("Overflow occurred: " + ex.Message);
}

// Suppressing overflow detection (unchecked)
int c = unchecked(a + 10);
Console.WriteLine("Value after addition (unchecked): " + c);

In the above example, adding 10 to the maximum value of an int would normally cause an overflow. In the checked block, this results in an OverflowException being thrown, whereas the unchecked block suppresses the check, allowing the overflowing result to be assigned to c.

Conclusion

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement Checked and Unchecked Keywords in C#

Understanding checked and unchecked Keywords

In C#, arithmetic operations that could potentially overflow or underflow are allowed by default. However, C# provides the checked and unchecked keywords to control overflow checking.

  • Checked: This keyword helps in catching potential overflow or underflow conditions. If an overflow or underflow occurs within a checked context, an OverflowException is thrown.

  • Unchecked: This keyword allows you to suppress overflow checking. Operations within an unchecked context do not throw exceptions even if they cause overflows.

Example Step-by-Step

Let's walk through an example to illustrate how to use checked and unchecked in C#.

Step 1: Setting Up Your Environment

First, ensure you have C# development environment ready. You can use Visual Studio, Visual Studio Code with the C# extension, or any other C# compatible IDE.

Step 2: Create a New C# Console Application

You can create a new console application in your IDE. Here’s the code for a simple demonstration.

using System;

namespace CheckedUncheckedExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 3: Demonstrating the unchecked keyword
            Console.WriteLine("Demonstrating unchecked keyword...");
            unchecked
            {
                int maxInt = int.MaxValue;
                int nextInt = maxInt + 1;
                Console.WriteLine($"Result of {maxInt} + 1 is {nextInt}");
            }

            // Step 4: Demonstrating the checked keyword for addition
            Console.WriteLine("\nDemonstrating checked keyword for addition...");
            try
            {
                int maxInt = int.MaxValue;
                int nextInt = checked(maxInt + 1); // This will throw an exception
            }
            catch (OverflowException ex)
            {
                Console.WriteLine($"Exception caught: {ex.Message}");
            }

            // Step 5: Demonstrating the checked keyword for subtraction
            Console.WriteLine("\nDemonstrating checked keyword for subtraction...");
            try
            {
                int minInt = int.MinValue;
                int prevInt = checked(minInt - 1); // This will throw an exception
            }
            catch (OverflowException ex)
            {
                Console.WriteLine($"Exception caught: {ex.Message}");
            }

            // Step 6: Using checked and unchecked in a larger scope
            Console.WriteLine("\nUsing checked and unchecked in a larger scope...");
            checked
            {
                int largeValue = 2100000000;
                int multiplier = 10;
                try
                {
                    int result = largeValue * multiplier; // This might overflow
                    Console.WriteLine($"Result of {largeValue} * {multiplier} is {result}");
                }
                catch (OverflowException ex)
                {
                    Console.WriteLine($"Exception caught: {ex.Message}");
                }
            }

            unchecked
            {
                int largeValue = 2100000000;
                int multiplier = 10;
                int result = largeValue * multiplier; // This might overflow but won't throw
                Console.WriteLine($"Result of {largeValue} * {multiplier} is {result}");
            }
        }
    }
}

Step 3: Demonstrating the unchecked Keyword

In this part, you perform an addition that will cause an overflow without raising an exception. The unchecked keyword is used to disable overflow checking.

Step 4: Demonstrating the checked Keyword for Addition

Here, you intentionally cause an integer overflow within a checked block. Since overflow is detected, an OverflowException is thrown, which is caught and handled in the catch block.

Step 5: Demonstrating the checked Keyword for Subtraction

Another demonstration is provided, this time subtracting 1 from int.MinValue which results in an underflow. Similar to the addition example, an OverflowException is thrown and caught.

Step 6: Using checked and unchecked in a Larger Scope

This part illustrates the use of checked and unchecked in a larger code block, showing the behavior for both multiplication scenarios.

Step 7: Run the Program

After writing the code, run the program in your development environment to see the results.

Output

You should see the following output:

Demonstrating unchecked keyword...
Result of 2147483647 + 1 is -2147483648

Demonstrating checked keyword for addition...
Exception caught: Arithmetic operation resulted in an overflow.

Demonstrating checked keyword for subtraction...
Exception caught: Arithmetic operation resulted in an overflow.

Using checked and unchecked in a larger scope...
Exception caught: Arithmetic operation resulted in an overflow.
Result of 2100000000 * 10 is 1728000000

Conclusion

By using checked and unchecked, you can control how your C# application handles arithmetic overflows and underflows. This can be particularly useful in scenarios where precise control over numerical operations is critical. It's a good practice to use checked blocks in contexts where overflow could lead to erroneous or unpredictable behavior.

You May Like This Related .NET Topic

Login to post a comment.