Advertisement
//C# The situation of overflow
using System;
class A
{
public static void Main(String[] ar)
{
//Creating local variables of type byte
byte b1, b2, result;
b1 = 20;
b2 = 30;
//Multiplying two byte values and their result is assigned to a byte variable
//This multiplication result exceeds the range of byte data type
//Hence casting is required.
result = (byte)(b1 * b2);
Console.WriteLine("Result: " + result);
}
}
Result: 88
checked(expression)
checked
{
//Statements to be checked
}
unchecked(expression)
unchecked
{
//Statements to be checked
}
//C# Using checked and unchecked keyword with an expression.
using System;
class A
{
public static void Main(String[] ar)
{
//Creating local variables of type byte
byte b1, b2, result;
b1 = 20;
b2 = 30;
//Multiplying the two byte values and assigning their result to a byte variable
//The result will be larger than the range of byte data type
//that's why casting has been performed
result = (byte)(b1 * b2);
Console.WriteLine("Result after casting: " + result);
//using the unchecked keyword for the same multiplication expression
//which will not check the overflow situation and that's why
//no exception of type OverflowException will be thrown in this overflow situation.
result = unchecked((byte)(b1 * b2));
Console.WriteLine("Result after using unchecked keyword: " + result);
//Using the checked keyword for the same multiplication expression
//the checked keyword will check the overflow situation and that's why
//an exception of type OverflowException will be thrown in this overflow situation.
result = checked((byte)(b1 * b2));
Console.WriteLine("Result after using unchecked keyword: " + result);
}
}
Result after casting: 88
Result after using unchecked keyword: 88
Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow.
at A.Main(String[] ar)
Advertisement
//C# Using checked and unchecked keyword with a block of statements.
using System;
class A
{
public static void Main(String[] ar)
{
//Creating local variables of type byte
byte b1, b2, result;
b1 = 20;
b2 = 30;
unchecked
{
//using the unchecked keyword for the multiplication expression
//which will not check the overflow situation and that's why
//no exception of type OverflowException will be thrown in this overflow situation.
result = unchecked((byte)(b1 * b2));
Console.WriteLine("Result after using checked keyword: " + result);
}
checked
{
//using the checked keyword for the same multiplication expression
//the checked keyword will check the overflow situation and that's why
//an exception of type OverflowException will be thrown in this overflow situation.
result = checked((byte)(b1 * b2));
Console.WriteLine("Result after using unchecked keyword: " + result);
}
}
}
Result after using checked keyword: 88
Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow.
at A.Main(String[] ar)
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement