c#-运算符-溢出ckeck运算符

1 溢出check 

1.1 运行结果

Hello World!
System.OverflowException: Arithmetic operation resulted in an overflow.
   at 运算符.溢出check运算符.main() in D:\projects\运算符\运算符\Program.cs:line 48

 1.2 代码

using System;

namespace 运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            new 溢出check运算符checked().main();
            Console.Read();
        }
    }

    class 溢出check运算符checked
    {
        public void main()
        {
            byte b = 255;
            checked
            {
                try
                {
                    b++;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
            Console.WriteLine(b);
        }
    }
    
}

2 溢出unCheck

2.1 运行结果

Hello World!
0

2.2 代码 

using System;

namespace 运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            new 溢出check运算符unchecked().main();
            Console.Read();
        }
    }

    class 溢出check运算符unchecked
    {
        public void main()
        {
            byte b = 255;
            unchecked
            {
                try
                {
                    b++;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
            Console.WriteLine(b);
        }
    }
}

 

你可能感兴趣的:(c#-函数式编程)