How to use Checked Operator in C#

No.of Views532
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  23 Feb 2011 01:02:32
Tag : .NET Frameworks , Miscellaneous
I am going to explain about the Checked Operator available in C#.NET to handle the integer overflow.
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

 

I am going to explain about the Checked Operator available in C#.NET to handle the integer overflow.

Problem

In my order management system, I have to calculate sales point for the each customer who places an order. This sales points are integer values which get earned by customer based on the product they purchase and get deducted from the account as they purchase new product using these points. But there are some customers who are not aware of point system or not consuming these points so that the value of point gets increased more than the limit of the integer values i.e., 2^32 . So whenever calculation takes place, I receive some junk value for those customers who are having point value more than integer value, i.e. 2^32.

Solution

To avoid this issue of overflow of integer value and to inform customer who is having more points, I came across a Checked Operator of C#.NET.

Checked Operator for Checking for Overflow in Mathematical Operations and conversions for the integer types.

Syntax

Checked( expression )
ss or
Checked { statements...... }

Example

Collapse
public static void Main()
  {int a;int b;int c;

   a = 2000000000;
   b = 2000000000;
   c = checked(a+ b);
   System.Console.WriteLine(Int1PlusInt2);
  }

When we run the above code, it throws an exception as below:

Collapse
Exception occurred: System.OverflowException: An exception of type 
System.OverflowException was thrown.

which tells that c value is more than the integer type length.

Same way in my application, I catch the overflow exception when thrown when calculating the order points and then raise mail to customer that you have to utilise the point you have, otherwise you will lose the points after end of xyz period and display point as maxvalue+ .

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    Let us start this article by a small chat between customer and developer. Scenario 1 Customer: - How’s your application performance? Subjective developer: - Well it’s speedy, it’s the best …huuh aaa ooh it’s a like rocket. Scenario 2 Customer: - How’s your application performance? Quantitative developer: - With 2 GB RAM , xyz processor and 20000 customer records the customer screen load in 20 secs. I am sure the second developer looks more promising than the first developer. In this
    Published Date : 10/May/2010
    Ask any developer which is the best place in a .NET class to clean unmanaged resources?, 70% of them will say the destructor. Although it looks the most promising place for cleanup it has a huge impact on performance and memory consumption. Writing clean up code in the destructor leads to double GC visits and thus affecting the performance multifold times. In order to validate the same we will first start with bit of theory and then we will actually see how GC algorithm performance is impacte
    Published Date : 06/May/2010
    One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET.
    Published Date : 05/May/2010
    This article will explain 6 important concepts Stack , heap , by val , by ref , boxing and unboxing. This article starts first explaining what happens internally when you declare a variable and then i
    Published Date : 02/May/2010
    Introduction Value Types in .Net Framework
    Published Date : 09/Apr/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