The Checked and Unchecked Operator
C# provides special operators, checked and unchecked. It enforces CLR to check overflow.
Consider the following code
- class Program
- {
- static void Main(string[] args)
- {
- byte b = 255;
- b++;
- Console.WriteLine(b);
- Console.ReadKey();
- }
- }

Byte data type range is 0 to 255 and in this code when we try to increase value then it loses value and it produces 0 as output. We have to find out any method through which we can get the exact value.
To do this we used checked operator. Consider the following code.

- class Program
- {
- static void Main(string[] args)
- {
- byte b = 255;
- checked
- {
- b++;
- }
- Console.WriteLine(b);
- Console.ReadKey();
- }
- }
Unchecked is the default behavior it will produce 0 as out put,
- unchecked
- {
- b++;
- }

Join the conversation! Your thoughts help the community grow.