Demonstates using checked keyword to detect an overflow : checked unchecked « Data Types « C# / C Sharp






Demonstates using checked keyword to detect an overflow

Demonstates using checked keyword to detect an overflow
// 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);
        }
    }

           
       








Related examples in the same category

1.OverflowCheck
2.Checked and Unchecked
3.Checking for overflows.
4.unchecked int overflow
5.Operators and Expressions:Checked and Unchecked Expressions
6.Demonstates using checked keyword to detect an overflow 2Demonstates using checked keyword to detect an overflow 2