文章目錄
一、表達式的定義
什麼是表達式:An expressions is a syntactic entity whose evaluation either produces a value
or fails to terminate
, in which case the expression is undefined.
……be evaluated to a
single value
,object
,method
, ornamespace
.
static void Main(string[] args)
{
//a single value
int x = 100;
x++;
++x;
//object
(new Form()).ShowDialog();
//method
Action ac = new Action(Console.WriteLine);//成員訪問表達式:類名.方法名—Console.WriteLine → 方法
//namespace
System.Windows.Forms.Form myForm = new Form();//名稱空間訪問表達式:System.Windows
}
二、各類表達式概覽
1.操作符組成的表達式
由這些操作符組成的表達式經過運算後,得到的值是什麼數據類型,表達式就是該類型。
(1) .
成員訪問操作符
namespace ExpressionSample
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
//成員訪問表達式stu.
var x = stu.ID;//整型
var y = stu.name;//字符串
}
}
class Student
{
public int ID;
public string name;
}
}
(2) f(x)
方法調用操作符
由方法調用組成的表達式,這個表達式的運算結果,就是方法調用所產生的返回值;
方法的返回值是什麼類型,表達式就是什麼類型的。
static void Main(string[] args)
{
var x = Math.Pow(2, 3);
Console.WriteLine(x.GetType().Name);//Double
}
(3) a[x]
元素訪問操作符
元素訪問表達式的數據類型,由集合的元素類型所決定。
static void Main(string[] args)
{
List<int> intList = new List<int>() { 1, 2, 3 };
double[] doubleArray = new double[] { 1.0, 2.0, 3.0 };
var x = intList[1];
var y = doubleArray[1];
Console.WriteLine(x.GetType().Name);//Int32
Console.WriteLine(y.GetType().Name);//Double
}
(4) x++
x--
後置自增、自減操作符
表達式的數據類型和操作數的數據類型是一致的。
static void Main(string[] args)
{
int x = 100;
Console.WriteLine(x++);//表達式的值100=操作數的值;運算完表達式後,操作數+1
Console.WriteLine(x);//操作數的值101
int y = 100;
Console.WriteLine(y--);//100
Console.WriteLine(y);//99
}
(5) ++x
--x
前置自增自減操作符
static void Main(string[] args)
{
int x = 100;
Console.WriteLine(++x);//101
Console.WriteLine(x);//101
int y = 100;
Console.WriteLine(--y);//99
Console.WriteLine(y);//99
}
(6) new
操作符
new操作符組成的表達式就是創建實例的表達式,這個表達式的數據類型就是實例的數據類型。
static void Main(string[] args)
{
Console.WriteLine((new Form()).GetType().Name);//Form
}
(7) typeof
操作符
返回值就是Type類型
(8) default
操作符
拿default操作的是什麼數據類型,表達式結果就是該類型。
static void Main(string[] args)
{
var x = default(Int32);
Console.WriteLine(x);
Console.WriteLine(x.GetType().Name);//Int32
}
(9) checked
& unchecked
操作符
表達式的數據類型和操作數的數據類型是一致的。
static void Main(string[] args)
{
var x = checked(111 + 222);
Console.WriteLine(x);
Console.WriteLine(x.GetType().Name);//Int32
}
(10)算術運算符
不發生類型提升的情況下,算術類型組成的表達式和它們的操作數數據類型是一樣的。
static void Main(string[] args)
{
//沒有發生類型提升
var x = 4 / 3;
Console.WriteLine(x.GetType().Name);//Int32
//發生類型提升
var y = 4.0 / 3;
Console.WriteLine(y.GetType().Name);//Double
}
(11)位移操作符
左移和右移,表達式的數據類型和操作符左邊的操作數類型一致。
static void Main(string[] args)
{
long x = 100;
Console.WriteLine((x<<2).GetType().Name);//Int64
}
(12)??
null合併操作符
表達式數據類型由null合併操作符 左邊操作數的 數據類型的 類型參數 決定。
同時,??左右兩邊類型不一致,會發生類型提升。
static void Main(string[] args)
{
Nullable<int> x = null;//int? x = null;
var y = x ?? 100;
Console.WriteLine(y.GetType().Name);//Int32
//類型提升
int? x = null;
var y = x ?? 100D;
Console.WriteLine(y.GetType().Name);//Double
}
(13)?:
條件操作符
static void Main(string[] args)
{
var x = 5 > 3 ? 2 : 3.0;//int:double → double
Console.WriteLine(x.GetType().Name);//double
}
(14)賦值操作符
static void Main(string[] args)
{
int x = 100;
int y;
Console.WriteLine(y = x);//100
Console.WriteLine((y = x).GetType().Name);//Int32
}
2.變量組成的表達式
static void Main(string[] args)
{
int x = 100;
int y;
y = x;
}
3.名稱空間組成的表達式
static void Main(string[] args)
{
System.Windows.Forms.Form myForm = new Form();
}
4.類型組成的表達式
static void Main(string[] args)
{
var t = typeof(Int32);
}
5.方法組 組成的表達式
Console.WriteLine,這是一組方法(19 種重載),重載決策決定具體調用那種方法。
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
6.null值表達式
static void Main(string[] args)
{
Form myForm = null;
}
7.匿名方法組成的表達式
static void Main(string[] args)
{
//匿名方法表達式,返回值-委託
Action a = delegate () { Console.WriteLine("Hello,World"); };
a();
}
8.屬性訪問組成的表達式
static void Main(string[] args)
{
Form myForm = new Form();
myForm.Text = "Hello";
myForm.ShowDialog();
}
9.事件訪問組成的表達式
class Program
{
static void Main(string[] args)
{
Form myForm = new Form();
//訪問屬性
myForm.Text = "Hlelo";
//訪問事件
myForm.Load += MyForm_Load;
myForm.ShowDialog();
}
private static void MyForm_Load(object sender, EventArgs e)
{
Form form = sender as Form;
if (form == null)
{
return;
}
else
{
form.Text = "New Title";
}
}
}
10.索引器訪問組成的表達式
static void Main(string[] args)
{
List<int> intList = new List<int>() { 1, 2, 3 };
int x = intList[2];
Console.WriteLine(x);
}
11.Nothing組成的表達式
Nothing 對返回值爲void的方法的調用
12.複合表達式的求值
C#語言定義文檔裏面未明確定義複合表達式,但確實常用。
注意操作符的優先級和同優先級操作符的運算方向(除了賦值操作符,基本都是從左向右)