TheDeveloperBlog.com

Home | Contact Us

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

C# Checked and Unchecked

C# Checked and Unchecked Exception for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Checked and Unchecked

C# provides checked and unchecked keyword to handle integral type exceptions. Checked and unchecked keywords specify checked context and unchecked context respectively. In checked context, arithmetic overflow raises an exception whereas, in an unchecked context, arithmetic overflow is ignored and result is truncated.

C# Checked

The checked keyword is used to explicitly check overflow and conversion of integral type values at compile time.

Let's first see an example that does not use checked keyword.

C# Checked Example without using checked

using System;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args) 
        {
                int val = int.MaxValue;
                Console.WriteLine(val + 2);
        }
    }
}

Output:

-2147483647

See, the above program produces the wrong result and does not throw any overflow exception.

C# Checked Example using checked

This program throws an exception and stops program execution.

using System;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args) 
        {
            checked
            {
                int val = int.MaxValue;
                Console.WriteLine(val + 2);
            }
        }
    }
}

Output:

Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow.

C# Unchecked

The Unchecked keyword ignores the integral type arithmetic exceptions. It does not check explicitly and produce result that may be truncated or wrong.

Example

using System;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args) 
        {
            unchecked
            {
                int val = int.MaxValue;
                Console.WriteLine(val + 2);
            }
        }
    }
}

Output:

-2147483647

Next TopicC# SystemException




Related Links:


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