1.运算符:
总体来说,我们可以把C#当中的运算符分为六种,分别是:算术运算符,三元运算符(条件运算符),逻辑运算符,比较运算符,赋值运算符,成员访问运算符.下边我们用几张表来俱体看一下每种运算符是如何体现的:
类别 | 运算符 | 说明 | 表达式 | 结果 |
算术运算符 |
+ | 用于执行加法运算 (如果两个操作数是字符串,则该运算符用作字符串连接运算符,将一个字符串添加到另一个字符串的末尾) |
1+2 |
3 |
- | 执行减法运算 | 5-3 | 2 | |
* | 执行乘法运算 | 2*3 | 6 | |
/ | 执行除法运算取商 | 6/3 | 2 | |
% | 执行除法运算取余数 | 7%3 | 1 | |
++ |
操作数加1 | i=3; j=i++; | 运算后,i的值是4,j的值是3 | |
i=3; j=++i; | 运算后,i的值是4,的值是4 | |||
-- |
操作数减1 | i=3; j=i--; | 运算后,i的值是2,j的值是3 | |
i=3; j=--i; | 运算后,i的值是2,j的值是2 | |||
比较运算符 | > | 检查一个数是否大于另一个数 | 6>5 | True |
< | 检查一个数是否小于另一个数 | 6<5 | False | |
>= | 检查一个数是否大于等于另一个数 | 6>=4 | True | |
<= | 检查一个数是否小于等于另一个数 | 6<=4 | False | |
== | 检查两个值是否相等 | “ab”==ab | True | |
!= | 检查两个数是否不等 | 5!=6 | True |
条件运算符 | ?: | 检查给出的表达式是否为真。如果为真,则运算结果为操作数1,否则运算结果为操作数2 | 表达式?操作数1:操作数2 |
|
赋 值 运 算 符 | = | 给变量赋值 | int a,b; a=1;b=a; | 运算后,b的值是1 |
+= | 操作数1与操作数2相加后赋值给操作数1 | int a,b; a=2;b=3; b+=a; | 运算后,b的值为5 | |
-= | 操作数1与操作数2相减后赋值给操作数1 | int a,b; a=2;b=3; b-=a; | 运算后,b的值为1 | |
*= | 操作数1与操作数2相乘后赋值给操作数1 | int a,b; a=2;b=3; b*=a; | 运算后,b的值为6 | |
/= | 操作数1与操作数2相除后赋值给操作数1 | int a,b; a=2;b=6; b/=a; | 运算后,b的值为3 | |
%= | 操作数1与操作数2相除取余后赋给操作数1 | int a,b; a=2;b=7; b%=a; | 运算后,b的值为1 | |
逻辑运算符 | && | 执行逻辑与运算,检查两个表达式是否都为真 | int a=5; (a<10)&&(a>5) | False |
|| | 执行逻辑或运算,检查两个表达式是否至少有一个为真 | int a=5; (a<10)||(a>5) | True | |
! | 执行逻辑非运算,检查特定表达式取反后是否为真 | bool result=true; !result; | Flase | |
类型转换 | () | 将一种数据类型强制转换为另一种数据类型 | (数据类型)操作数 |
|
操作数类型 | typeof | 表示某种数据类型 | typeof(string) |
|
二、常见的表达式及运算符
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
一元运算符(++/--)及表达式
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三元运算符及表达式
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
三元运算符还是常用滴.希望能多练练.
is操作符是用来动态的检测运行时对像类型是否与指定类型 兼容,运算结果 返回一个bool值,例:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
需要注意的是,我们能表述的意思是 苹果是水果,得到的答案为true.
as操作符:
as操作符应用于 兼容的引用类型之间的转换.返回值为值本身或null.如:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)