Using checked and unchecked

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Using checked and unchecked. 
 
using System; 
 
public class CheckedDemo {  
  public static void Main() {  
    byte a, b; 
    byte result; 
 
    a = 127; 
    b = 127; 
  
    try {  
      result = unchecked((byte)(a * b)); 
      Console.WriteLine("Unchecked result: " + result); 
 
      result = checked((byte)(a * b)); // this causes exception 
      Console.WriteLine("Checked result: " + result); // won't execute 
    }  
    catch (OverflowException exc) {  
      // catch the exception  
      Console.WriteLine(exc); 
    }  
  }  
}