Demonstates using checked keyword to detect an overflow

image_pdfimage_print

   

// Compile this program with the following command line:
// C:>csc /checked OvrFlow2.cs

    using System;
    public class OvrFlow2
    {
        static public void Main ()
        {
            int large = 2147483647;
            int larger = large;
            unchecked
            {
                ++larger;
                larger *= 2;
            }
            Console.WriteLine ("large = " + large);
            Console.WriteLine ("larger = " + larger);
        }
    }

           
          


This entry was posted in Data Types. Bookmark the permalink.