基本: .  ()  []  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();
    }
}


posted on 2008-12-30 11:53  万一  阅读(2738)  评论(4编辑  收藏  举报