Use of Checked and Unchecked keyword in C#

Here I am going to explain what the use of checked and unchecked keyword in C#. Here I am taking the some content from MSDN to explain about it. A checked context, arithmetic overflow raises an exception. In an unchecked context, arithmetic overflow is ignored and the result is truncated.

  • Checked Specify checked context.
  • unchecked Specify unchecked context.

The following operations are affected by the overflow checking:

  • Expressions using the following predefined operators on integral types:
    ++ — - (unary) + - * /
  • Explicit numeric conversions between integral types.

The checked/unchecked compiler option lets you specify checked or unchecked context for all integer arithmetic statements that are not explicitly in the scope of a checked or unchecked keyword. Now I am going to explain one by one.

Checked

The checked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. It can be used as an operator or a statement according to the following forms.

The checked statement:

checked block

The statement block that contains the expressions to be evaluated in a checked context.

The checked operator:

checked (expression)

The expression to be evaluated in a checked context. Notice that the expression must be in parentheses ( ).

Unchecked

The unchecked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. It can be used as an operator or a statement according to the following forms.

The unchecked statement :

unchecked block

The statement block that contains the expressions to be evaluated in an unchecked context.

The unchecked operator :

unchecked (expression)

The expression to be evaluated in an unchecked context. Notice that the expression must be in parentheses ( ).

Example

namespace Checked_Unchecked

{

    class Program

    {

        public short a = 30000;

        public short b = 20000;

        public short c;

 

        public int Add()

        {

            try

            {

                c = checked((short)(a + b));

 

            }

            catch (System.OverflowException e)

            {

                System.Console.WriteLine(e.ToString());

            }

            return c;

        }

 

        public int Mul()

        {

            try

            {

                checked

                {

                    c = (short)(a * b);

                }

            }

            catch (System.OverflowException e)

            {

                System.Console.WriteLine(e.ToString());

            }

            return c;

        }

 

        public int Add_Unchecked()

        {

            try

            {

                c = unchecked((short)(a + b));

 

            }

            catch (System.OverflowException e)

            {

                System.Console.WriteLine(e.ToString());

            }

            return c;

        }

 

        public int Mul_Unchecked()

        {

            try

            {

                unchecked

                {

                    c = (short)(a * b);

                }

            }

            catch (System.OverflowException e)

            {

                System.Console.WriteLine(e.ToString());

            }

            return c;

        }

 

        static void Main(string[] args)

        {

            Program p = new Program();

 

            // For checked

            Console.WriteLine("Checked output value is: {0}", p.Add());

            Console.WriteLine("Checked output value is: {0}", p.Mul());

            // For Unchecked

            Console.WriteLine("Checked output value is: {0}", p.Add_Unchecked());

            Console.WriteLine("Checked output value is: {0}", p.Mul_Unchecked());

            Console.ReadKey(true);

        }

    }

}

Output


Image1.jpg