So when we assign a value to a variable then that variable should be able to hold it, right? and if not compiler produces the error. Now there is an option in C# known as Checked (which is by default) and unchecked option. But is there practical use of unchecked? any comments for a layman? 🙂
Example:
int a=int.MaxValue;
int b=int.MaxValue;
unchecked
{
int sum=a+b;
}
The practical use of unchecked is that it’s faster.
The default for non-constant expressions is unchecked (unless specified otherwise by the compiler or execution environment). It’s one of the few times C# chooses performance at the cost of safety. Since the majority of code never gets anywhere near the limits it is rarely an issue in practice.