Csharp/C Sharp/Data Types/checked unchecked

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Checked and Unchecked

using System;
using System.Collections.Generic;
using System.Text;
class Program {
    static void Main(string[] args) {
        Console.WriteLine("Max value of byte is {0}.", byte.MaxValue);
        Console.WriteLine("Min value of byte is {0}.", byte.MinValue);
        byte b1 = 100;
        byte b2 = 250;
        try {
            checked {
                byte sum = (byte)(b1 + b2);
                byte b4, b5 = 100, b6 = 200;
                b4 = (byte)(b5 + b6);
                Console.WriteLine("sum = {0}", sum);
            }
        } catch (OverflowException e) {
            Console.WriteLine(e.Message);
        }
        unchecked {
            byte sum = (byte)(b1 + b2);
            Console.WriteLine("sum = {0}", sum);
        }
    }
}


Checking for overflows.

 

using System;
public class MainClass {
    public int CheckedAddition(short s1, short s2) {
        int z = 0;
        try {
            z = checked((short)(s1 + s2));
        } catch (OverflowException) {
            Console.WriteLine("Overflow Exception, Returning 0");
        }
        return z;
    }
    public int UncheckedAddition(short s1, short s2) {
        int z = ((short)(s1 + s2));
        return z;
    }
    public static void Main() {
        MainClass app = new MainClass();
        short s1 = 32767;
        short s2 = 32767;
        Console.WriteLine("Checked Addition is: {0}",app.CheckedAddition(s1, s2));
        Console.WriteLine("Unchecked Addition is: {0}",app.UncheckedAddition(s1, s2));
    }
}


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);
        }
    }


Demonstates using checked keyword to detect an overflow 2

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
//  OvrFlow1.cs -- Demonstates using checked keyword to detect an overflow.
//
//          Compile this program with the following command line:
//              C:>csc OvrFlow1.cs
//
namespace nsOverflow
{
    using System;
    
    public class OvrFlow1
    {
        static public void Main ()
        {
            int large = 2147483647;
            int larger = large;
            try
            {
                larger = checked (++larger);
            }
            catch (OverflowException e)
            {
                Console.WriteLine ("The operation caused an overflow");
                Console.WriteLine (e.Message);
            }
            Console.WriteLine ("large = " + large);
            Console.WriteLine ("larger = " + larger);
        }
    }
}


Operators and Expressions:Checked and Unchecked Expressions

using System;
public class CheckedandUncheckedExpressions
{
    public static void Main()
    {
        unchecked
        {
            byte a = 55;
            byte b = 210;
            byte c = (byte) (a + b);
        }
    }
}


OverflowCheck

 
using System;
using System.Collections.Generic;
using System.Text;
class Program {
    static void Main(string[] args) {
        byte destinationVar;
        short sourceVar = 281;
        destinationVar = checked((byte)sourceVar);
        Console.WriteLine("sourceVar val: {0}", sourceVar);
        Console.WriteLine("destinationVar val: {0}", destinationVar);
    }
}


unchecked int overflow

 
using System;
   
class IntegerOverFlowConst
{
    static void Main()
    {
        const ushort MAXUSHORT = 65535;
        const ushort ONE = 1;
   
        unchecked
        {
            ushort total = (short)MAXUSHORT + ONE;
        }
    }
}