Lesson 6.5

Checked and Unchecked Statement C# - Programming Example


Objective

What is checked and unchecked keyword in C#?
Programming Example

What is checked and unchecked statement in c#?

Checked and Unchecked keyword in c# affects the arithmetic overflow exception. I know you didn’t understand anything but don’t worry. Here I am going to explain it in a very simple way. But before understanding it, you need to know what arithmetic overflow exception is.

Arithmetic overflow exception

Arithmetic overflow exception occurs when a variable stores value beyond its limitation. For example a byte data type can store value up to 255. What happened if we multiply two numbers and store in it a byte variable as follow:

byte x=30;
byte y=10;
byte z=x*y;

In this condition the multiplication result, which is 300 is more than byte range. By default all variable is unchecked so the real value would be truncated and you will get wrong result as output or get arithmetic overflow exception. To overcome this situation the checked keyword developed.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Checked_Unchecked
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, result;
            num1 = int.MaxValue;
            Console.WriteLine("Maximum value of Int is " + num1);
            Console.WriteLine("\n\nUnchecked Result\n\n");
            //Unchecked Statement Example
            try
            {                
                unchecked
                {
                    result = num1 + 10;
                    Console.WriteLine("{0} + 10 = {1}",num1,result);
                }                
            }
            catch(System.OverflowException e)
            {
                Console.WriteLine("UNCHECKED Result  " + e.ToString());
            }

            Console.WriteLine("\n\n--------------------------\n\n");

            Console.WriteLine("Checked Result\n\n");
            //Checked Statement Example
            try
            {
                checked
                {
                    result = num1 + 10;
                    Console.WriteLine("{0} + 10 = {1}", num1, result);
                }
            }
            catch (System.OverflowException e)
            {
                Console.WriteLine("CHECKED Result  " + e.ToString());
            }

            Console.ReadKey();
        }
    }
}

Output

Maximum value of Int is 2147483647

Unchecked Result

2147483647 + 10 = -2147483639

--------------------------

Checked Result

CHECKED Result System.OverflowException: Arithmetic operation resulted in an overflow. at Checked_Unchecked.Program.Main(String[] args) in c:\Users\Prashant\Documents\Visual Studio 2012\Projects\OtherStatements\Checked_Unchecked\Checked_Unchecked\Program.cs:line 39

__

Summary

In this chapter you have learned what the checked and unchecked statement is and how to use it in programming language. In the next chapter you will learn about Enumeration in C#.

BackNext