Checked and Unchecked Conversions in C#

No.of Views2614
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Aug 2010 09:08:51
Tag : CSharp , General
In this article, I will discuss about Checked and unchecked keyword and conversions in C#.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, 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. 

Image Loading

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 

Image Loading

So, now data will overflow and truncate during the assignment.  So, as the output we will get 

Image Loading

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 .

 

Image Loading

So, now to avoid truncating during overflow assignment, we need to put our code in between Checked block.
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. 

Image Loading

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 

Image Loading

You can change the behavior of compiler from checked to unchecked through command prompt.that;s all about checked and unchecked keyword in c#.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    I can see, there is lots of people is discussing about it in MSDN forums, few people wanted to get rid of the Task from the async methods and really want to deal with normal return types, while others just wanted to get rid of the async postfix
    Published Date : 25/Nov/2010
    Are you somewhat confused between Serialization and Marshaling? This writing would break this confusion up, it would give you a basic understanding of the process of Serialization and the process of Marshaling, and how you can get the most out of each.
    Published Date : 10/May/2010
    First, this writing concentrates of and compares between three programming languages, C#, C++/CLI, and ISO/ANSI C++. It discusses 9 rules that every developer should keep in mind while working with constructors, destructors, and finalizers and class hierarchies:
    Published Date : 05/May/2010
    This article explains how to filter rows in a DataSet/DataTable. The example provided will help you get information faster.
    Published Date : 23/May/2010
    In this article, I will explain three basic terms of C#, such as Call Stack ,Call Site and stack Unwinding
    Published Date : 17/Aug/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top