checked和unchecked

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    public class Program
    {
        static short x = 32767;
        static short y = 32767;

        //检查溢出
        public static int F()
        {
            int z = checked((short)(x + y));
            return z;
        }

        public static int H()
        {
            int z = unchecked((short)(x + y));
            return z;
        }

        static void Main(string[] args)
        {

            Console.WriteLine("不检查溢出:\t{0}", H());

            Console.WriteLine("检查溢出:\t{0}", F());
        }
    }
}

 

你可能感兴趣的:(F#)