TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Unchecked Context

This C# example program demonstrates the unchecked keyword. Unchecked affects how overflow is handled.

Unchecked specifies overflow behavior.

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

Tip: This feature specifies that overflow is an acceptable result of an operation such as increment—no exception is thrown.

Example. 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 uses the short type, which cannot exceed the value 32767.

Short

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. The value will become invalid for most programs.

C# program that uses unchecked context

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 program shows both the checked and unchecked keywords. It is compiled by default in the unchecked context. We specify the unchecked context to make it clear that the second increment (b++) is not supposed to be checked.

Our program displays two numbers when the exception is thrown. The exception occurs because of the checked context. The a++ statement exceeds the bounds of the short type. The checked statement resulted in the highest valid short value.

And: The value of the b variable shows that overflow occurred and the short was incremented past its bounds and became negative.

Checked context. 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.

And: You do not need to specify unchecked unless you have specified an enclosing checked context.

Checked

Tip: It is a good idea to use unchecked explicitly in cases where you are also using checked. This provides more symmetry.

Summary. The unchecked context changes program semantics from the checked context when incrementing values. This context is the default mode in which programs are compiled using Microsoft's C# compiler.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf