C# Unchecked Context

The C# programming language

Numeric types cause no exceptions when they overflow. This is the default behavior in C# programs. It is also specified with the unchecked keyword. Unchecked overrides the checked keyword or a checked default setting.

Keywords

Key point: This feature provides a way to specify that overflow is an acceptable result of an operation such as increment, and that no exception should be thrown in this case.

Example

Note

The unchecked keyword designates a section of code as not throwing exceptions when you assign, decrement or increment a variable past its bounds. The program text uses the short type, which cannot exceed the value 32767.

In a checked context, an increment when the value is 32767 will throw an exception, but in an unchecked context, an increment will cause an overflow and the value will become invalid for most programs.

Program that uses unchecked context [C#]

using System;

class Program
{
    static void Main()
    {
	// The first short will overflow after the second short does.
	short a = 0;
	short b = 100;
	try
	{
	    //
	    // Keep incrementing the shorts until an exception is thrown.
	    // ... This is terrible program design.
	    //
	    while (true)
	    {
		checked
		{
		    a++;
		}
		unchecked
		{
		    b++;
		}
	    }
	}
	catch
	{
	    // Display the value of the shorts when overflow exception occurs.
	    Console.WriteLine(a);
	    Console.WriteLine(b);
	}
    }
}

Output

32767
-32669
This section provides information

Description. This program demonstrates a use of both the checked and unchecked keywords in the C# language. The C# program is compiled by default in the unchecked context, but we specify the unchecked context to make it clear that the second increment (b++) is not supposed to be checked, which is not the same semantics we want for the (a++) statement in the checked context.

Output. The program displays two numbers when the exception is thrown. The exception occurs because of the checked overflow context and the a++ statement exceeds the bounds of the short type. The final values printed by the program show that the checked statement resulted in the highest logically correct value to increment a short to. The value of the b variable shows that overflow occurred and the short was incremented past its bounds and became negative.

Checked context

Programming tip

The checked context is more often useful than the unchecked context when you are specifying the keywords explicitly. This is because the unchecked context is the default when compiling C# programs in the Microsoft compiler. You do not need to specify unchecked unless you have specified an enclosing checked context. It is a good idea to use unchecked explicitly in cases where you are also using checked. This provides more symmetry.

Checked Context

Summary

Check illustration

We looked at the unchecked context in the C# language. It changes program semantics from the checked context when incrementing values. The unchecked context is the default mode in which programs are compiled using Microsoft's C# compiler, which means you will likely use the unchecked keyword even less often than the checked context. However, it provides an essential symmetry.

Exception Handling
.NET