Advertisement



< Prev
Next >



C# Checked and Unchecked Keywords





In C#, a situation when the result of an arithmetic expression exceeds the range of the data type to which this arithmetic result is assigned, leads to a problem called overflow. To curb such situation of overflow of data, we perform casting. Let us see an example.




An example of overflow


//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);
}
}


Output


Result: 88

As you can see in the output, the correct result of multiplying 20 with 30 is 600 and not 88, but because the multiplication result had to be assigned to a byte data type variable, therefore the casting was performed, which casted the correct result(600) to a value(88), which was in the range of the byte data type.

C# allows you to specify whether or not such situation of overflow should throw an exception of type OverflowException, by providing you two keywords - checked and unchecked. Let us understand about these keywords.




The checked keyword


The checked keyword is used when we want the overflow situation to throw an exception of type OverflowException. The checked keyword has two forms, let us look at their general forms:






The unchecked keyword


The unchecked keyword is used when we do not want to check the overflow situation i.e. no exception of type OverflowException will be thrown in the overflow situation. The unchecked keyword has two forms, let us look at their general forms:






Example of checked and unchecked keyword with an expression


In the upcoming coding example, we will use the checked and unchecked keywords involving the same multiplication expression as in our previous example, and see what happens.

//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);
}
}


Output


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)

As you can see in the output:


Advertisement




Example of checked and unchecked keyword with a block of statements


In the upcoming coding example, we will use the checked and unchecked keywords with a block of statements containing the the same multiplication expression(as in the last example) and see what happens.

//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);
	}
}
}


Output


Result after using checked keyword: 88

Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow.
   at A.Main(String[] ar)

As you can see in the output:



Please share this article -





< Prev
Next >
< C# Throw Keyword
C# User Defined Exception >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement