Public · Protected · Private
c# checked ,unchecked words
Type: Public  |  Created: 2012-06-12  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • while doing a type casting .... there is chance of overflow or underflow . byte a1 = 222; byte a2= 212; byte sum = checked((byte)Add(a1, a2)); chances are that we get bad value for sum ( 222+212-256) we can tell compiler to be pricky about such happenings. (now you try and catch the overflow) try { byte sum = checked((byte)Add(a1, a2)); //or you can write //checked //{ // byte sum = (byte)Add(a1, a2); // Console.WriteLine("sum = {0}", sum); //} } catch (OverflowException ex) { Console.WriteLine(ex.Message); }
    2012-06-12 07:05
  • as reverse you can explicitly want to ignore as below.. unchecked { byte sum = (byte)(a1 + a2); } You will do the above when -- you already have a project wide setting Overflow
    2012-06-12 07:07
This blog is frozen. No new comments or edits allowed.