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