.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Checked and Unchecked Conversions Tutorial

Posted By:Dhananjay Kumar       Posted Date: August 13, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

In this tutorial, I will discuss about Checked and unchecked keyword and conversions in C# with an example.
 

Objective 


In this tutorial, I will discuss about Checked and unchecked keyword and conversions in C#. 


Consider the below code 


Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication7
{
   class Program
   {
       static void Main(string[] args)
       {
               int number = int.MaxValue;
               number = number + 1;
               Console.WriteLine(number);
               Console.Read();
       }
   }
}

Now, if you see the above code 
  1. We are taking maximum integer value in a variable.  And that is 2147483647.

    1.gif

  2. We are increasing the number by 1. Now here it should get overflow  because an integer variable cannot hold a number greater than  2147483647

    2.gif

So, now data will overflow and truncate during the assignment. 


So, as the output we will get 


3.gif

If you notice above output, number assigned is truncated to -2147483647


It happened because compiler is doing unchecked compilation 


So forcefully do the checked compilation, we will use CHECKED keyword.  Just put all the codes in between checked block 
So, now to avoid truncating during overflow assignment, we need to put our code in between Checked block. 


4.gif



So, modify the above code as below 


Programs.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication7
{
   class Program
   {
       static void Main(string[] args)
       {
           checked
           {
               int number = int.MaxValue;
               number = number + 1;
               Console.WriteLine(number);
               Console.Read();
           }
       }
   }
}


Now if you run the above code, you will get the below error. 


5.gif

It shows if the code is in checked block then rather than truncating at overflow, compiler is throwing an exception. 


If we are doing checked compilation then to override that, we can put our code in Unchecked Block 


6.gif

You can change the behavior of compiler from checked to unchecked through command prompt.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend