Tuesday, 2 October 2018

checked and unchecked in c#


Basically Checked and unchecked are operators of C#.
Checked and Unchecked operators enforce CLR ( Common Language Runtime ) to handle stack overflow control.

These operators checks code block which can cause overflow in an application.

Checked operator
unchecked operator


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 statement.

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 determined in an unchecked statement.

The unchecked operator :

unchecked (expression)

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

Example
namespace Checked_Uncheckedkeyword
{

    class Program
    {

        public short a = 40000;
        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);
        }
    }
}



No comments:

Post a Comment