How to use checked and unchecked keyword in C#
Checked and unchecked keyword in C#
In this article we will learn what is checked and unchecked
keyword and will learn how to use them. Before starting with Check and uncheck keyword we
have to know the range of any data type. For example if we use Int32 as data
type then what the maximum number we can able to store in this data type. Let’s
see in example.
Checked keyword
In this example we will see the maximum and minimum range of
Int32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Minimum Value :- " + Int32.MinValue);
Console.WriteLine("Maximum Value:- " + Int32.MaxValue);
Console.ReadLine();
}
}
}
In Int32
class MinValue and MaxValue properties are available to detect maximum and minimum
value of Integer variable. Here is the output

In output,
we are seeing the maximum and minimum value of Int32. Now in next example we will add two integers where
the result will be more than actual maximum capacity of integer variable. Have
a look on below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern
{
class Program
{
static void Main(string[] args)
{
Int32 a = 1234567890;
Int32 b = 1234567890;
Int32 c = a + b;
Console.ReadLine();
}
}
}
Here is
output screen.

But it’s
showing negative value where we are adding two positive values. Actually the
resultant value is out of maximum range so it’s showing some garbage value (Actually
there is logic behind it and we can evaluate why this negative value is coming)
. Now ,in application if we perform same type of operation it will show wrong
result and program will show unexpected result. We can use check keyword to
prevent this problem. Have a look on below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern
{
class Program
{
static void Main(string[] args)
{
Int32 a = 1234567890;
Int32 b = 1234567890;
Int32 c = checked(a + b);
Console.WriteLine("Value of C is= " + c);
Console.ReadLine();
}
}
}
Here
before to initialize variable c we are checking the resultant value using
checked keyword. When we will try to run this application, we will see this
error.

So, it
will not allow us to initialize more value than maximum value of a data type.
Unchecked keyword
Uncheck is
just opposite of checked keyword. If we use uncheck then it will not check the
resultant range and maximum range of data type. Here is one example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPattern
{
class Program
{
static void Main(string[] args)
{
Int32 a = 1234567890;
Int32 b = 1234567890;
Int32 c = unchecked(a + b);
Console.WriteLine("Value of C is= " + c);
Console.ReadLine();
}
}
}

It’s
showing negative result, so it does not check range validation.
Conclusion:-
In this article we have learned how to use checked and unchecked keyword in C# to validate range of data type with value. Hope you have understood the concept.