逻辑(布尔型和按位) & | ^ ! ~ && || true false
字符串连接 +
递增、递减 ++ --
移位 << >>
关系 == != < > <= >=
赋值 = += -= *= /= %= &= |= ^=
成员访问 .
索引 []
转换 ()
条件 ?:
委托连接和删除 + -
对象创建 new
类型信息 as is sizeof typeof
溢出异常控制 checked unchecked
间接寻址和地址 * -> [] &
· 浮点算术溢出或被零除从不引发异常,因为浮点类型基于IEEE 754,因此可以表示无穷和 NaN(非数字)。
· decimal 算术溢出总是引发 OverflowException 异常,decimal 被零除总是引发 DivideByZeroException 异常。
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];
h["a"] = 123; // 使用字符串作为索引
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];
}
语法:
(type) expr
type 表示expr转换后类型的名称。
expr 表示一个表达式。
例如下面的类:
{
public int x;
public int y;
{
}
}
Exam s = new Exam();
s.y = 9; // 给域b赋值
using 指令使某些名称限定变成可选:
using System;
// ...
System.Console.WriteLine("hello");
Console.WriteLine("hello"); // 结果同上
// ...
System.Console.WriteLine( "hello" ); // 必须限定 Console类
// 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类型
}
}
10
5.5
55
55
using System;
class MainClass
{
static void Main()
{
int a = 5;
Console.WriteLine(-a);
Console.WriteLine(a - 1);
Console.WriteLine(a - .5);
}
}
4
4.5
// 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
// 需要 /unsafe 编译选项
public class MainClass
{
unsafe static void Main()
{
int i = 5;
int* j = &i;
System.Console.WriteLine(*j);
}
}
5
using System;
class MainClass
{
static void Main()
{
Console.WriteLine(-5/2);
Console.WriteLine(-5.0/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
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
// 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
// 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
// cs_operator_negation.cs
using System;
class MainClass
{
static void Main()
{
Console.WriteLine(!true);
Console.WriteLine(!false);
}
}
False
True
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
// 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
// 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
// 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
using System;
class MainClass
{
static void Main()
{
Console.WriteLine(1 <= 1.1);
Console.WriteLine(1.1 <= 1.1);
}
}
True
True
using System;
class MainClass
{
static void Main()
{
Console.WriteLine(1.1 >= 1);
Console.WriteLine(1.1 >= 1.1);
}
}
True
True
x += y
等效于:
x = x + y
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
x -= y
等效于:
x = x - y
using System;
class MainClass
{
static void Main()
{
int a = 5;
a -= 6;
Console.WriteLine(a);
}
}
x *= y
等效于:
x = x * y
using System;
class MainClass
{
static void Main()
{
int a = 5;
a *= 6;
Console.WriteLine(a);
}
}
x /= y
等效于:
x = x / y
using System;
class MainClass
{
static void Main()
{
int a = 5;
a /= 6;
Console.WriteLine(a);
double b = 5;
b /= 6;
Console.WriteLine(b);
}
}
0.833333333333333
x %= y
等效于:
x = x % y
using System;
class MainClass
{
static void Main()
{
int a = 5;
a %= 3;
Console.WriteLine(a);
}
}
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