The “checked” keyword is used to define a scope in which
arithmetic operations will be checked for overflow.
'uncheck' This keyword defines a scope in which check of arithmetic overflow is disabled.
It makes sense to use it in projects in which the checking for overflow is
enabled for an entire project (can be set on the project level settings).
using System;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
checked
{
int val = int.MaxValue;
Console.WriteLine(val + 2);
}
}
}
}
The Unchecked keyword ignores the integral type arithmetic exceptions.
It does not check explicitly and produce result that may be truncated or wrong.
using System;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
unchecked
{
int val = int.MaxValue;
Console.WriteLine(val + 2);
}
}
}
}