C# 操作符

  C# 语言中有大量的操作符,这些操作符是指定在表达式中执行操作的符号,C# 中预定义了算术操作符、逻辑操作符及其它的操作符,有些可以重载,在应用到自定义类型时其意义可以改变。
操作符分类表
操作符类型      操作符                      
算术         +   -   *   /   %
逻辑(布尔型和按位) &   |   ^   !   ~   &&   ||   true   false
字符串连接      +
递增、递减      ++   --
移位         <<   >>
关系         ==   !=   <   >   <=   >=
赋值         =   +=   -=   *=   /=   %=   &=   |=   ^=
           <<=   >>=   ??
成员访问       .
索引         []
转换         ()
条件         ?:
委托连接和删除    +   -
对象创建       new
类型信息       as   is   sizeof   typeof
溢出异常控制     checked   unchecked
间接寻址和地址    *   ->   []   &
1. 算术溢出
  使用算术运算符(+、-、*、/)可能会产生超出调用的数值类型可以存放的数值范围。通常有以下几种情况:
  · 整数算术溢出或者产生 OverflowException 异常,或者丢弃结果的最高有效位。整数被零除将引发 DivideByZeroException 异常。
  · 浮点算术溢出或被零除从不引发异常,因为浮点类型基于IEEE 754,因此可以表示无穷和 NaN(非数字)。
  · decimal 算术溢出总是引发 OverflowException 异常,decimal 被零除总是引发 DivideByZeroException 异常。
  当发生整数溢出时,产生的结果取决于执行环境,执行环境可为 checked 或 unchecked。在 checked 环境下,将产生 OverflowException 异常。在 unchecked 环境下,将放弃结果的最高有效位并继续执行。因此,C# 使您有机会选择处理或忽略溢出。
  除算术运算符以外,整型之间的强制转换也会导致溢出(例如,将 long 强制转换为 int),溢出的结果也取决于 checked 或 unchecked 的执行环境。然而,按位运算符和移位运算符不会产生溢出。
2. [] 操作符
  方括号([])用于数组、索引器和属性,也可用于指针。
  数组类型是在类型的后面加上 []。例如:
    int [] myarray;      //myarray是一个整数数组。
    myarray = new int[100];    //创建100个元素的整数数组。
  若要访问数组中的元素,将元素的索引放到方括号中。例如:
    myarray[0] = myarray[1] = 1;
    for (int i=2; i<100; ++i) myarray[i] = myarray[i-1] + myarray[i-2];
  如果数组索引超出范围,将会产生异常。
  不能重载数组索引运算符;但类型可以定义采用一个或多个参数的索引器和属性。索引器参数括在方括号中(就像数组索引一样),但索引器参数可声明为任何类型(这与数组索引不同,数组索引必须为整数)。
  例如,.NET Framework 中定义了一个 Hashtable 类型,它的键可以和任意类型的数值关联。
    Collections.Hashtable h =  new Collections.Hashtable();
    h["a"] = 123;    // 使用字符串作为索引
  方括号还用于指定属性:
    [attribute (AllowMultiple=true)]
    public class Attr
    {
    }
  可以使用方括号来指定指针索引:
    unsafe fixed ( int* p = myarray )   // p指向前面例子中的myarray
    {
        p[0] = p[1] = 1;
        for( int i=2; i<100; ++i ) p[i] = p[i-1] + p[i-2];
    }
  不执行边界检查。
3. () 操作符
  圆括号除了用于指定表达式中的运算顺序外,还用于指定强制转换或类型转换。
语法:
    (type) expr
  其中:
  type 表示expr转换后类型的名称。
  expr 表示一个表达式。
  类型转换会显示调用实现expr的类型到type类型的转换操作符,如果未定义这样的操作符,转换就会失败。
  不能重载 () 运算符。
4. . 操作符
  点运算符(.)用于成员访问。点运算符指定类型或命名空间的成员。
例如下面的类:
    class Exam
    {
        public int x;
        public int y;
        {
        }
    }
    Exam s = new Exam();
  变量Exam包含x和y两个成员,要访问它们,就需要使用点操作符。
    s.x = 6;     // 给赋a赋值
    s.y = 9;     // 给域b赋值
  点还用于构造限定名,即指定其所属的命名空间或接口的名称。
    System.Console.WriteLine("hello");    // 类 Console 在名称空间 System 中
  using 指令使某些名称限定变成可选:
    using System;
    // ...
    System.Console.WriteLine("hello");
    Console.WriteLine("hello");   // 结果同上
  但是当某一标识符不确定时,必须限定它:
    using System;
    using OtherSystem;     // 另一个也包含 Console 类的名称空间
    // ...
    System.Console.WriteLine( "hello" );     // 必须限定 Console类
5. + 操作符
  + 操作符既可作为一元操作符也可作为二元操作符。
  一元 + 操作符是为所有数值类型预定义的。对数值类型进行一元 + 运算的结果就是操作数的值。
  为数值类型和字符串类型预定义了二元 + 操作符。对于数值类型,+ 计算两个操作数之和。当其中的一个或两个操作数是字符串类型时,+ 将操作数的字符串表示形式串联在一起。
  委托类型也提供二元 + 操作符,它执行委托连接。
  用户定义的类型可以重载一元 + 和二元 + 操作符。在枚举时通常允许整型运算。
  示例:
// cs_operator_plus.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(+5);        // 一元 +
        Console.WriteLine(5 + 5);     // 加法
        Console.WriteLine(5 + .5);    // 加法
        Console.WriteLine("5" + "5"); // 字符串连接
        Console.WriteLine(5.0 + "5"); // 字符串连接
        // 注意:自动由double类型转换为string类型
    }
}
  输出:
5
10
5.5
55
55
6. - 操作符
  - 操作符既可作为一元操作符也可作为二元操作符。
  一元 - 操作符是为所有数值类型预定义的。数值类型的一元 - 运算的结果是操作数的相反数。
  二元 - 运算符是为所有数值类型和枚举类型预定义的,用于从第一个操作数中减去第二个操作数。
  委托类型也提供了二元 - 操作符,它执行委托删除操作。
  用户定义的类型可以重载一元 - 和二元 - 操作符。
  示例:
// cs_operator_minus.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 5;
        Console.WriteLine(-a);
        Console.WriteLine(a - 1);
        Console.WriteLine(a - .5);
    }
}
  输出:
-5
4
4.5
7. * 操作符
  乘法操作符(*)用于计算两个操作数的积。另外还用作取消引用操作符,允许读取和写入指针。
  所有数值类型都具有预定义的乘法操作符。
  * 操作符还用来声明指针类型和取消引用指针。该运算符只能在不安全的环境中使用,通过 unsafe 关键字的使用来表示,并且需要 /unsafe 编译器选项。取消引用操作符也称为间接寻址操作符。
  用户定义的类型可重载 * 操作符。重载二元 * 操作符时,也会隐式重载相应的赋值操作符(如果有)。
  示例:
// cs_operator_mult.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(5 * 2);
        Console.WriteLine(-.5 * .2);
        Console.WriteLine(-.5m * .2m); // decimal type
    }
}
  输出:
10
-0.1
-0.10
  示例:
// cs_operator_ptr.cs
// 需要 /unsafe 编译选项
public class MainClass
{
    unsafe static void Main()
    {
        int i = 5;
        int* j = &i;
        System.Console.WriteLine(*j);
    }
}
  输出:
5
 
8. / 操作符
  除法操作符(/)用第二个操作数除第一个操作数。所有数值类型都具有预定义的除法操作符。
  用户自定义的类型可重载 / 操作符。重载 / 操作符将隐式重载 /=操作符。
  示例:
// cs_operator_division.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(-5/2);
        Console.WriteLine(-5.0/2);
    }
}
  输出:
-2
-2.5


9. % 操作符
  模数操作符(%)用来获取第一个操作数除以第二个操作数后得到的余数。所有数值类型都具有预定义的模数操作符。
  用户定义的类型可重载 % 操作符。重载二元 % 运算符时,也会隐式重载相应的赋值运算符(如果有)。
  示例:
// cs_operator_modulus.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(5 % 2);         // int
        Console.WriteLine(-5 % 2);        // int
        Console.WriteLine(5.0 % 2.2);     // double
        Console.WriteLine(5.0m % 2.2m);   // decimal
        Console.WriteLine(-5.2 % 2.0);    // double
    }
}
  输出:
1
-1
0.6
0.6
-1.2
10. & 操作符
  & 运算符既可用作一元操作符也可用作二元操作符。一元 & 操作符返回操作数的地址(要求 unsafe 环境)。
  整型和 bool 类型具有预定义的二进制 & 运算符。对于整型,& 计算两个操作数的逻辑按位“与”。对于 bool 操作数,& 计算操作数的逻辑“与”,只有当两个操作数均为 true 时结果才为 true。
  & 操作符计算两个操作数,与第一个操作数的值无关。例如:
int i = 0;
if (false & ++i == 1)
{
    // i 递增1,但条件表达式的结果为 false,
    // 因此,这个代码块不被执行。
}
  用户自定义的类型可以重载二元 & 操作符。重载二元运算符时,也会隐式重载相应的赋值运算符(如果有)。
  示例:
// cs_operator_ampersand.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(true & false);    // 逻辑“与”
        Console.WriteLine(true & true);     // l逻辑“与”
        Console.WriteLine("0x{0:x}", 0xf8 & 0x3f);     // 按位“与”
    }
}
  输出:
False
True
0x38
11. | 操作符
  整型和 bool 类型具有预定义的二元 | 运算符。对于整型,| 计算两个操作数的按位“或”结果。对于 bool 操作数,| 计算操作数的逻辑“或”结果;只有当两个操作数均为 false 时,结果才为 false。
  用户自定义的类型可重载 | 运算符。
  示例:
// cs_operator_OR.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(true | false);     // 逻辑“或”
        Console.WriteLine(false | false);    // 逻辑“或”
        Console.WriteLine("0x{0:x}", 0xf8 | 0x3f);    // 按位“或”
    }
}
  输出:
True
False
0xff
12. ^ 操作符
  整型和 bool 类型具有预定义的二元 ^ 操作符。对于整型,^ 计算两个操作数的按位“异或”。对于 bool 操作数,^ 将计算操作数的逻辑“异或”;当且仅当只有一个操作数为 true 时结果才为 true。
  用户定义的类型可重载 ^ 运算符。在枚举时通常允许整型运算。
  示例:
// cs_operator_bitwise_OR.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(true ^ false);      // 逻辑“异或”
        Console.WriteLine(false ^ false);     // 逻辑“异或”
        Console.WriteLine("0x{0:x}", 0xf8 ^ 0x3f);   // 按位“异或”
    }
}
  输出:
True
False
0xc7
13. ! 操作符
  逻辑非操作符(!)是对操作数求反的一元操作符,只能用于 bool 类型,当且仅当操作数为 false 时才返回 true。
  用户定义的类型可重载 ! 操作符。
  示例:
// cs_operator_negation.cs
using System;
class MainClass
{
    static void Main()
{
        Console.WriteLine(!true);
        Console.WriteLine(!false);
    }
}
  输出:
False
True
14. ~ 操作符
  ~ 操作符对操作数执行按位求补运算,其效果相当于反转每一位。int、uint、long 和 ulong 类型具有预定义的按位求补操作符。
  用户定义的类型可重载 ~ 操作符。在枚举时通常允许整型运算。
  示例:
// cs_operator_bitwise_compl.cs
using System;
class MainClass
{
    static void Main()
    {
        int[] values = { 0, 0x111, 0xfffff, 0x8888, 0x22000022};
        foreach (int v in values)
        {
            Console.WriteLine("~0x{0:x8} = 0x{1:x8}", v, ~v);
        }
    }
}
  输出:
~0x00000000 = 0xffffffff
~0x00000111 = 0xfffffeee
~0x000fffff = 0xfff00000
~0x00008888 = 0xffff7777
~0x22000022 = 0xddffffdd
15. =操作符
  赋值操作符(=)将右操作数的值存储到左操作数表示的存储位置、属性或索引器中,并将值作为结果返回。两个操作数的类型必须相同(或右边的操作数必须可以隐式转换为左边操作数的类型)。
  不能重载赋值运算符。
  示例:
// cs_operator_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        double x;
        int i;
        i = 5;    // int 到 int 赋值
        x = i;    // int 隐式转换为 double
        i = (int)x;    // 必须强制转换
        Console.WriteLine("i is {0}, x is {1}", i, x);
        object obj = i;
        Console.WriteLine("boxed value = {0}, type is {1}",
                  obj, obj.GetType());
        i = (int)obj;
        Console.WriteLine("unboxed: {0}", i);
    }
}
  输出:
i is 5, x is 5
boxed value = 5, type is System.Int32
unboxed: 5
16. < 操作符
  所有数值和枚举类型都定义了“小于”关系操作符(<),如果第一个操作数小于第二个操作数时返回 true,否则返回 false。
  用户定义的类型可重载 < 运算符。如果重载了 <,则还必须重载 >。重载二元运算符时,也会隐式重载相应的赋值运算符(如果有)。
  示例:
// cs_operator_less_than.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(1 < 1.1);
        Console.WriteLine(1.1 < 1.1);
    }
}
  输出:
True
False
17. > 操作符
  所有数值类型和枚举类型都定义了“大于”关系操作符(>),如果第一个操作数大于第二个操作数时返回 true,否则返回 false。
  用户定义的类型可重载 > 操作符。如果重载了 > ,则还必须重载 < 。
  示例:
// cs_operator_greater_than.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(1.1 > 1);
        Console.WriteLine(1.1 > 1.1);
    }
}
  输出:
True
False

18. ?: 操作符

  条件操作符(?:)根据第一个数值返回后两个数值中的一个。
  语法:
    cond-expr ? expr1 : expr2;
  其中:
  cond-expr:bool类型的表达式。
  expr1:表达式1。
  expr2:表达式2。

  如果 cond-expr 为 true,expr1 被计算作为结果;如果为 cond-expr 为 false,则 expr2 被计算作为结果。只计算两个表达式中的一个。
  不能重载条件运算符。

  使用条件运算符,可以更简洁、雅观地表达那些否则可能要求 if-else 结构的计算。
  例如,为在 sin 函数的计算中避免被零除,可编写为:
    if(x != 0.0) s = Math.Sin(x)/x; else s = 1.0;
或使用条件运算符:
    s = x != 0.0 ? Math.Sin(x)/x : 1.0;

  条件运算符为右联运算符,因此该形式的表达式:
    a ? b : c ? d : e
按如下规则计算:
    a ? b : (c ? d : e)
而不是按照下面这样计算:
    (a ? b : c) ? d : e
  示例:
// cs_operator_conditional.cs
using System;
class MainClass
{
    static double sinc(double x)
    {
        return x != 0.0 ? Math.Sin(x)/x : 1.0;
    }

    static void Main()
    {
        Console.WriteLine(sinc(0.2));
        Console.WriteLine(sinc(0.1));
        Console.WriteLine(sinc(0.0));
    }
}

  输出:
0.993346653975306
0.998334166468282
1

19. ++ 操作符

  递增操作符(++)将操作数加1。递增操作符可以出现在操作数之前或之后。
  语法:
    ++var
    var++

  其中:
  var 表示存储位置、属性或索引的表达式。

  第一种形式是前缀递增操作符,操作的结果是操作数加 1 之后的值。
  第二种形式是后缀递增操作符,操作的结果是操作数增加之前的值。
  数值类型和枚举类型具有预定义的递增操作符。用户定义的类型可重载 ++ 运算符。
  示例:
// cs_operator_increment.cs
using System;
class MainClass
{
    static void Main()
    {
        double x;
        x = 1.5;
        Console.WriteLine(++x);
        x = 1.5;
        Console.WriteLine(x++);
        Console.WriteLine(x);
    }
}
  输出:
2.5
1.5
2.5

20. -- 操作符

  递减操作符(--)将操作数减 1。递减操作符可以出现在操作数之前或之后。
  语法:

    --var
    var--

  其中:
  var 表示存储位置、属性或索引的表达式。

  第一种形式是前缀递减操作符,操作的结果是操作数减小之后的值。
  第二种形式是后缀递减操作符,操作的结果是操作数减小之前的值。
  数值类型和枚举类型具有预定义的增量运算符。
  用户定义的类型可重载 -- 运算符。
  示例:

// cs_operator_decrement.cs
using System;
class MainClass
{
    static void Main()
    {
        double x;
        x = 1.5;
        Console.WriteLine(--x);
        x = 1.5;
        Console.WriteLine(x--);
        Console.WriteLine(x);
    }
}
  输出:
0.5
1.5
0.5

21. && 操作符

  条件“与”操作符(&&)对两个 bool 操作数进行逻辑“与”运算,但仅在必要时才计算第二个操作数。
  操作“x && y”对应于操作“x & y”。不同的是,如果 x 为 false,将不计算 y(因为不论 y 为何值,“与”操作的结果都为 false)。这被称为“短路”计算。
  不能重载条件“与”运算符,但普通逻辑操作符和操作符 true 与 false 的重载,在某些限制条件下也被视为条件逻辑运算符的重载。

  示例:在下面的示例中,请观察使用 && 的表达式只计算第一个操作数。
// cs_operator_logical_and.cs
using System;
class MainClass
{
    static bool Method1()
    {
        Console.WriteLine("Method1 called");
        return false;
    }

    static bool Method2()
    {
        Console.WriteLine("Method2 called");
        return true;
    }

    static void Main()
    {
        Console.WriteLine("regular AND:");
        Console.WriteLine("result is {0}", Method1() & Method2());
        Console.WriteLine("short-circuit AND:");
        Console.WriteLine("result is {0}", Method1() && Method2());
    }
}

  输出:
regular AND:
Method1 called
Method2 called
result is False
short-circuit AND:
Method1 called
result is False

22. || 操作符

  条件“或”操作符(||)对两个 bool 操作数进行逻辑“或”运算,但仅在必要时才计算第二个操作数。
  操作“x || y”对应于操作“x | y”。不同的是,如果 x 为 true,将不计算 y(因为不论 y 为何值,“或”操作的结果都为 true)。这被称作“短路”计算。
  不能重载条件“或”操作符,但普通逻辑操作符和操作符 true 与 false 的重载,在某些限制条件下也被视为条件逻辑操作符的重载。

  示例:在下面的示例中,请观察使用 || 的表达式只计算第一个操作数。
// cs_operator_short_circuit_OR.cs
using System;
class MainClass
{
    static bool Method1()
    {
        Console.WriteLine("Method1 called");
        return true;
    }

    static bool Method2()
    {
        Console.WriteLine("Method2 called");
        return false;
    }

    static void Main()
    {
        Console.WriteLine("regular OR:");
        Console.WriteLine("result is {0}", Method1() | Method2());
        Console.WriteLine("short-circuit OR:");
        Console.WriteLine("result is {0}", Method1() || Method2());
    }
}

  输出:
regular OR:
Method1 called
Method2 called
result is True
short-circuit OR:
Method1 called
result is True

23. << 操作符

  左移操作符(<<)将第一个操作数按位向左移动第二个操作数指定的位数。第二个操作数的类型必须是 int。
  语法:
    expr << count
  其中:
  expr 表示 int、uint、long 或 ulong 类型的表达式,要移位的数值。
  count 表示 int 类型的表达式,移位的位次数。
  如果 expr 是 int 或 uint(32位数),则移位数由 count 的低 5 位给出(count & 0x1f)。
  如果 expr 是 long 或 ulong(64 位数),则移位数由 count 的低 6 位给出(count & 0x3f)。
  expr 的高序位被放弃,低序空位用 0 填充。移位操作不会导致溢出。
  用户定义的类型可重载 << 操作符;第一个操作数的类型必须为用户定义的类型,第二个操作数的类型必须为 int。重载二元运算符时,也会隐式重载相应的赋值运算符(如果有)。

  示例:
// cs_operator_left_shift.cs
using System;
class MainClass
{
    static void Main()
    {
        int i = 1;
        long lg = 1;
        Console.WriteLine("0x{0:x}", i << 1);
        Console.WriteLine("0x{0:x}", i << 33);
        Console.WriteLine("0x{0:x}", lg << 33);
    }
}

  输出:
0x2
0x2
0x200000000

  注意,i<<1 和 i<<33 给出相同的结果,因为 1 和 33 的低序 5 个位相同。

24. >> 操作符

  右移操作符(>>)将第一个操作数按位向右移动第二个操作数所指定的位数。
  语法:
    expr >> count
  其中:
  expr 表示 int、uint、long 或 ulong 类型的表达式,要移位的数值。
  count 表示 int 类型的表达式,移位位数。
  如果 expr 为 int 或 uint(32位数),则移位数由 count 的低 5 位给出(count & 0x1f)。
  如果 expr 为 long 或 ulong(64位数),则移位数由 count 的低 6 位给出(count & 0x3f)。
  如果 expr 为 int 或 long,则右移位是算术移位(高序空位设置为符号位)。如果第一个操作数为 uint 或 ulong 类型,则右移位是逻辑移位(高位填充 0 )。
  用户定义的类型可重载 >> 操作符;第一个操作数的类型必须为用户定义的类型,第二个操作数的类型必须为 int。重载二元运算符时,也会隐式重载相应的赋值运算符(如果有)。

  示例:
// cs_operator_right_shift.cs
using System;
class MainClass
{
    static void Main()
    {
        int i = -1000;
        Console.WriteLine(i >> 3);
    }
}

  输出:
-125

25. == 操作符

  对于预定义的值类型,如果两个操作数的值相等,则相等操作符(==)返回 true,否则返回 false。对于 string 以外的引用类型,如果两个操作数引用同一个对象,则 == 返回 true。对于 string 类型,== 比较两个字符串的值。
  用户定义的值类型可重载 == 操作符。用户定义的引用类型也可重载 == 操作符。如果重载 ==,则还必须重载 !=。

  示例:
// cs_operator_equality.cs
using System;
class MainClass
{
    static void Main()
    {
        // Numeric equality: True
        Console.WriteLine((2 + 2) == 4);

        // Reference equality: different objects,
        // same boxed value: False.
        object s = 1;
        object t = 1;
        Console.WriteLine(s == t);

        // Define some strings:
        string a = "hello";
        string b = String.Copy(a);
        string c = "hello";

        // Compare string values of a constant and an instance: True
        Console.WriteLine(a == b);

        // Compare string references;
        // a is a constant but b is an instance: False.
        Console.WriteLine((object)a == (object)b);

        // Compare string references, both constants
        // have the same value, so string interning
        // points to same reference: True.
        Console.WriteLine((object)a == (object)c);
    }
}

  输出:
True
False
True
False
True

26. != 操作符

  如果操作数相等,则不等操作符(!=)返回 false,否则返回 true。所有类型(包括字符串和对象)都有预定义的不等操作符。用户定义的类型可重载 != 操作符。
  对于预定义的值类型,如果操作数的值不同,不等操作符(!=)返回 true,否则返回 false。对于 string 以外的引用类型,如果两个操作数引用不同的对象,则 != 返回 true。对于 string 类型,!= 比较字符串的值。
  用户定义的值类型可重载 != 操作符。用户定义的引用类型也可重载 != 操作符,默认情况下,无论对于预定义的引用类型还是用户定义的引用类型,!= 的行为都与上面描述的相同。如果重载 !=,则还必须重载 ==。

  示例:
// cs_operator_inequality.cs
using System;
class MainClass
{
    static void Main()
    {
        // Numeric inequality:
        Console.WriteLine((2 + 2) != 4);

        // Reference equality: two objects, same boxed value
        object s = 1;
        object t = 1;
        Console.WriteLine(s != t);

        // String equality: same string value, same string objects
        string a = "hello";
        string b = "hello";

        // compare string values
        Console.WriteLine(a != b);

        // compare string references
        Console.WriteLine((object)a != (object)b);
    }
}

  输出:
False
True
False
False

27. <= 操作符
  所有数值和枚举类型都定义了“小于等于”关系操作符(<=)。如果第一个操作数小于或等于第二个操作数,将返回 true,否则返回 false。
  用户定义的类型可重载 <= 操作符。如果重载 <=,则还必须重载 >=。
  示例:
// cs_operator_less_than_or_equal.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(1 <= 1.1);
        Console.WriteLine(1.1 <= 1.1);
    }
}
  输出:
True
True
28. >= 操作符
  所有数值类型和枚举类型都定义了“大于等于”关系操作符(>=)。如果第一个操作数大于或等于第二个操作数,将返回 true,否则返回 false。
  用户定义的类型可重载 >= 操作符。如果重载 >=,则还必须重载 <=。
  示例:
// cs_operator_greater_than_or_equal.cs
using System;
class MainClass
{
    static void Main()
    {
        Console.WriteLine(1.1 >= 1);
        Console.WriteLine(1.1 >= 1.1);
    }
}
  输出:
True
True
29. += 操作符
  加法赋值操作符。
  使用 += 赋值操作符的表达式,例如:
    x += y
等效于:
    x = x + y
  不同的是 x 只计算一次。+ 操作符的含义取决于 x 和 y 的类型(对于数值操作数,其含义为相加;对于字符串操作数,其含义为连接)。
  不能直接重载 += 运算符,但用户定义的类型可以重载 + 运算符。
  示例:
// cs_operator_addition_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 5;
        a += 6;
        Console.WriteLine(a);
        string s = "Micro";
        s += "soft";
        Console.WriteLine(s);
    }
}
  输出:
11
Microsoft
30. -= 操作符
  减法赋值操作符。
  使用 -= 赋值操作符的表达式,如:
    x -= y
等效于:
    x = x - y
  不同的是 x 只计算一次。- 操作符的含义取决于 x 和 y 的类型(例如,对于数值类型操作数,其含义为相减;对于委托类型操作数,其含义为删除)。
  不能直接重载 -= 操作符,但用户定义的类型可重载 - 操作符。
  示例:
// cs_operator_subtraction_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 5;
        a -= 6;
        Console.WriteLine(a);
    }
}
  输出:
-1
31. *= 操作符
  乘法赋值操作符。
  使用 *= 赋值操作符的表达式,如:
    x *= y
等效于:
    x = x * y
  不同的是 x 只计算一次。为数值类型预定义了 * 操作符以执行乘法操作。
  不能直接重载 *= 操作符,但用户定义的类型可重载 * 操作符。
  示例:
// cs_operator_multiplication_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 5;
        a *= 6;
        Console.WriteLine(a);
    }
}
  输出:
30
32. /= 操作符
  除法赋值操作符。
  使用 /= 赋值操作符的表达式,如:
    x /= y
等效于:
    x = x / y
  不同的是 x 只计算一次。为数值类型预定义了 / 操作符以执行除法操作。
  不能直接重载 /= 操作符,但用户定义的类型可重载 / 操作符。对于所有复合赋值操作符,隐式重载二元操作符会重载等效的复合赋值操作符。
  示例:
// cs_operator_division_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 5;
        a /= 6;
        Console.WriteLine(a);
        double b = 5;
        b /= 6;
        Console.WriteLine(b);
    }
}
  输出:
0
0.833333333333333
33. %= 操作符
  模数赋值操作符。
  使用 %= 赋值操作符的表达式,如:
    x %= y
等效于:
    x = x % y
  不同的是 x 只计算一次。为数值类型预定义了 % 操作符,以计算相除后的余数。
  不能直接重载 %= 操作符,但用户定义的类型可重载 % 操作符。
  示例:
// cs_operator_modulus_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 5;
        a %= 3;
        Console.WriteLine(a);
    }
}
  输出:
2

 

34. &= 操作符

  “与”赋值操作符。
  使用 &= 赋值操作符的表达式,如:
    x &= y
  等效于:
    x = x & y

  不同的是 x 只计算一次。& 操作符对整数类型操作数执行按位逻辑“与”运算,对 bool 类型操作数执行逻辑“与”运算。

  不能直接重载 &= 操作符,但用户定义的类型可重载二元 & 运算符。

  示例:
// cs_operator_and_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 0x0c;
        a &= 0x06;
        Console.WriteLine("0x{0:x8}", a);
        bool b = true;
        b &= false;
        Console.WriteLine(b);
    }
}

  输出:
0x00000004
False

35. |= 操作符

  “或”赋值操作符。
  使用 |= 赋值操作符的表达式,例如:
    x |= y
等效于:
    x = x | y

  不同的是 x 只计算一次。| 操作符对整型操作数执行按位逻辑“或”运算,对布尔操作数执行逻辑“或”运算。

  不能直接重载 |= 操作符,但用户定义的类型可以重载 | 操作符。

  示例:
// cs_operator_or_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 0x0c;
        a |= 0x06;
        Console.WriteLine("0x{0:x8}", a);
        bool b = true;
        b |= false;
        Console.WriteLine(b);
    }
}

  输出:
0x0000000e
True

36. ^= 操作符

  “异或”赋值操作符。
  使用 ^= 赋值操作符的表达式,例如:
    x ^= y
等效于:
    x = x ^ y

  不同的是 x 只计算一次。^ 操作符对整数操作数执行按位“异或”运算,对 bool 操作数执行逻辑“异或”运算。

  不能直接重载 ^= 操作符,但用户定义的类型可重载 ^ 操作符。

  示例:
// cs_operator_xor_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 0x0c;
        a ^= 0x06;
        Console.WriteLine("0x{0:x8}", a);
        bool b = true;
        b ^= false;
        Console.WriteLine(b);
    }
}

  输出:
0x0000000a
True

37. <<= 操作符

  左移赋值操作符。
  使用 <<= 赋值操作符的表达式,例如:
    x <<= y
等效于:
    x = x << y

  不同的是 x 只计算一次。<< 操作符将 x 左移 y 指定的位数。

  不能直接重载 <<= 操作符,但用户定义的类型可重载 << 操作符。

  示例:
// cs_operator_left_shift_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 1000;
        a <<= 4;
        Console.WriteLine(a);
    }
}

  输出:
16000

38. >>= 操作符

  右移赋值操作符。
  使用 >>= 赋值操作符的表达式,例如:
    x >>= y
等效于:
    x = x >> y

  不同的是 x 只计算一次。>> 操作符将 x 右移 y 指定的位数。

  不能直接重载 >>= 操作符,但用户定义的类型可重载 >> 操作符。

  示例:
// cs_operator_right_shift_assignment.cs
using System;
class MainClass
{
    static void Main()
    {
        int a = 1000;
        a >>= 4;
        Console.WriteLine(a);
    }
}

  输出:
62

39. -> 操作符

  -> 操作符将指针取消引用与成员访问组合在一起。
  以下形式的表达式:
    x->y
(其中 x 为 T* 类型的指针,y 为 T 的成员)等效于:
    (*x).y

  -> 操作符只能在非托管代码中使用。

  不能重载 -> 操作符。

  示例:
// cs_operator_dereferencing.cs
// compile with: /unsafe
using System;
struct Point
{
    public int x, y;
}

class MainClass
{
    unsafe static void Main()
    {
        Point pt = new Point();
        Point* pp = &pt;
        pp->x = 123;
        pp->y = 456;
        Console.WriteLine ( "{0} {1}", pt.x, pt.y );
    }
}

  输出:

123 456

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值