• C# 语法练习(3): 运算符



    基本: .  ()  []  x++  x--  new  typeof  checked  unchecked  ->  ::
    一元: +  -  !  ~  ++x  --x  (T)x  True  False  &  sizeof
    乘除: *  /  %
    加减: +  -
    移位: <<  >>
    关系: <  >  <=  >=  is  as  
    相等: ==  !=
    逻辑: &  ^  |
    条件: &&  ||
    赋值: =  +=  -=  *=  /=  %=  &=  |=  ^=  <<=  >>=  
    选择: ?:  ??
    其他: =>
    

    整数 / 整数 = 整数
    using System;
    
    class MyClass
    {
        static void Main()
        {
            double d;
            d = 14 / 4;
            Console.WriteLine(d);    //3
            d = 14 / 4.0;
            Console.WriteLine(d);    //3.5
            d = 14.0 / 4.0;
            Console.WriteLine(d);    //3.5
            d = 14.0 / 4;
            Console.WriteLine(d);    //3.5
    
            Console.WriteLine();
    
            float f;
            f = 14 / 4;
            Console.WriteLine(f);    //3
            f = (float)(14.0 / 4.0); /* 默认返回 double, 因而需要转换 */
            Console.WriteLine(f);    //3.5
    
            Console.WriteLine();
    
            int i;
            i = 14 / 4;
            Console.WriteLine(i);    //3
            i = (int)(14.0 / 4.0);
            Console.WriteLine(i);    //3
            i = 14 % 4;
            Console.WriteLine(i);    //2
    
            Console.ReadKey();
        }
    }
    

    ++ -- 可以对 double 类型
    using System;
    
    class MyClass
    {
        static void Main()
        {
            double f = 1.5;
            f++; Console.WriteLine(f); //2.5
            f--; Console.WriteLine(f); //1.5
            Console.WriteLine(++f);    //2.5
            Console.WriteLine(--f);    //1.5
    
            Console.ReadKey();
        }
    }
    

    ?:
    using System;
    
    class MyClass
    {
        static void Main()
        {
            int n, a=11, b=22;
    
            n = a > b ? a : b;
            Console.WriteLine(n); //22
            n = a < b ? a : b;
            Console.WriteLine(n); //11
    
            Console.ReadKey();
        }
    }
    

    ??
    using System;
    
    class MyClass
    {
        static void Main()
        {
            int? x = null;        /* 给变量赋 null 的写法, 一般用于数值和布尔类型 */
            int y;
    
            y = x ?? -1;          /* 如果 x 为 null 将返回后者, 反之返回 x */
            Console.WriteLine(y); // -1
    
            x = 9;
            y = x ?? -1;
            Console.WriteLine(y); // 9
    
            Console.ReadKey();
        }
    }
    

    =>
    using System;
    using System.Linq;
    
    class MyClass
    {
        static void Main()
        {
            int n1, n2;
    
            int[] ns = {22, 333, 4444, 9};
            n1 = ns.Max(num => num);
            n2 = ns.Min(num => num);
            Console.WriteLine("{0}, {1}", n1, n2);       //4444, 9
    
            string[] ss = {"aaa", "bbbb", "ccccc", "dd"};
            n1 = ss.Max(str => str.Length);
            n2 = ss.Min(str => str.Length);
            Console.WriteLine("{0}, {1}", n1, n2);       //5, 2
    
            Console.ReadKey();
        }
    }
    

    & | ^ !
    using System;
    
    class MyClass
    {
        static void Main()
        {
            bool b;
    
            b = true & true;   Console.WriteLine(b);  // True
            b = true & false;  Console.WriteLine(b);  // False
            b = false & false; Console.WriteLine(b);  // False
    
            b = true | true;   Console.WriteLine(b);  // True
            b = true | false;  Console.WriteLine(b);  // True
            b = false | false; Console.WriteLine(b);  // False
    
            b = true ^ true;   Console.WriteLine(b);  // False
            b = true ^ false;  Console.WriteLine(b);  // True
            b = false ^ false; Console.WriteLine(b);  // False
    
            b = !true;         Console.WriteLine(b);  // False
            b = !false;        Console.WriteLine(b);  // True
    
            Console.ReadKey();
        }
    }
    

    && ||; 在条件判断时, 应尽量使用 &&、|| 而不是 &、|; 因为后者总是要计算出结果, 因而会慢.
    using System;
    
    class MyClass
    {
        static void Main()
        {
            bool b;
    
            /* 在前两种情形下, && 不再判断其后的值 */
            b = false && true;  Console.WriteLine(b);  // False
            b = false && false; Console.WriteLine(b);  // False
            b = true && true;   Console.WriteLine(b);  // True
            b = true && false;  Console.WriteLine(b);  // False
    
            /* 在前两种情形下, || 不再判断其后的值 */
            b = true || true;   Console.WriteLine(b);  // True
            b = true || false;  Console.WriteLine(b);  // True
            b = false || true;  Console.WriteLine(b);  // True
            b = false || false; Console.WriteLine(b);  // False
    
            Console.ReadKey();
        }
    }
    

  • 相关阅读:
    前端面试题总结(js、html、小程序、React、ES6、Vue、算法、全栈热门视频资源)
    steps 步骤条、时间轴
    碰撞检测经典解决方案
    延迟渲染(Deferred Shading)技术详解
    配备透明触摸屏 看3D全息投影概念手机
    预渲染技术
    APK改之理(APK IDE)修改APK简单的入门教程
    如何获取显卡的GPU占用率和显存占用情况
    图像编辑之对比度调整 亮度对比度的算法公式
    别被你的双眼所欺骗 100张神奇的视觉欺骗图
  • 原文地址:https://www.cnblogs.com/del/p/1365107.html
Copyright © 2020-2023  润新知