Why we Use Checked and Unchecked Operators in C#

This article explains the use of checked operators but you need to first understand the purpose of them.

  1. Checked Operator: When due to a limitation of a datatype, like an int, we lose the data or get the unpredictable value, for skipping this garbage value and generate the "Stackoverflow exception" , use a checked operator.
     
  2. Unchecked Operator: it is the reverse of a checked operator where you get the garbage value in the actual scenario. It is used by default in C#.

How you can handle the unpredictable/garbage value but first you need to understand the default scenario. To do that you need to do use the following procedure.

Step 1: Create a console application named "Console Application1".



Step 2: Write the code where you have 2 int variables that has a value such as 200,000 and 300,000. Now store the multiplication of both values into an int variable as in the following:

try

{

   
int a = 200000;
   
int b = 300000;
   
int c = (a * b);
   
Console.WriteLine(c.ToString());
   
Console.ReadKey();
}

catch
(Exception ex)
{}




Step 3: If I run the code above I will get an unpredictable value, in other words I will lose the data.



Reason: It happens because the maximum length of an int is "2,14,74,83,648" and the multiplication of both variable will be "60,00,00,00,000" so the int c variable can't hold the multiplied value and gives the garbage value instead.

Question: In this scenario, if the user wants the multiplication to exceed the limit of an int, then it will give the message, otherwise it works fine. So now what will you do?

Solution: You can use the "checked" operator that will throw the exception when the multiplication exceeds the limit of the int.

Try the following 2 cases:

  1. When it exceeds the limitation of an int:

                try

                {

                    int a = 200000;

                    int b = 300000;

                    int c = checked(a * b);

                    Console.WriteLine(c.ToString());

                    

                }

                catch(Exception ex)

                {

                    Console.WriteLine("Your multiplication cross the limit");

                }


    Console.ReadKey();



    In the code above the Checked operator generates the exception and the catch block catches it and prints an appropriate message.

    Now run it.


     
  2. When it doesn't exceed the limitation of an int:

                try

                {

                    int a = 20000;

                    int b = 30000;

                    int c =checked(a * b);

                    Console.WriteLine(c.ToString());               

                }

                catch(Exception ex)

                {

                    Console.WriteLine("Your multiplication cross the limit");

                }

                Console.ReadKey();




Now I changed the value of both variables "a" and "b" that will generates the value that exists in the range of an int and runs it.



Note: But if I use an "unchecked" operator when it exceeds the limitation of an int then it gives a garbage value because it is used by default in C#.

            try

            {

                int a = 2000000;

                int b = 3000000;

                int c =unchecked(a * b);

                Console.WriteLine(c.ToString());               

            }

            catch(Exception ex)

            {

                Console.WriteLine("Your multiplication cross the limit");

            }

            Console.ReadKey();