文章目录
一、表达式的定义
什么是表达式: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#语言定义文档里面未明确定义复合表达式,但确实常用。
注意操作符的优先级和同优先级操作符的运算方向(除了赋值操作符,基本都是从左向右)