运算符(Operator)用于对变量、常量和表达式进行操作,是 C# 编程中的基础知识。
1. 算术运算符
用于数学计算。
| 运算符 | 名称 | 示例 | 结果 |
|---|---|---|---|
| + | 加法 | 5 + 3 | 8 |
| - | 减法 | 5 - 3 | 2 |
| * | 乘法 | 5 * 3 | 15 |
| / | 除法 | 5 / 2 | 2 |
| % | 取余 | 5 % 2 | 1 |
示例
cs
int a = 10;
int b = 3;
Console.WriteLine(a + b); //13
Console.WriteLine(a - b); //7
Console.WriteLine(a * b); //30
Console.WriteLine(a / b); //3
Console.WriteLine(a % b); //1
2. 自增与自减运算符
++(自增)
int a = 5;
a++;
Console.WriteLine(a); //6
等价于:
a = a + 1;
--(自减)
int a = 5;
a--;
Console.WriteLine(a); //4
前置与后置区别
前置++
cs
int a = 5;
int b = ++a;
Console.WriteLine(a); //6
Console.WriteLine(b); //6
先加后赋值
后置++
cs
int a = 5;
int b = a++;
Console.WriteLine(a); //6
Console.WriteLine(b); //5
先赋值后加
3. 赋值运算符
| 运算符 | 说明 |
|---|---|
| = | 赋值 |
| += | 加后赋值 |
| -= | 减后赋值 |
| *= | 乘后赋值 |
| /= | 除后赋值 |
| %= | 取余后赋值 |
示例:
cs
int a = 10;
a += 5; //15
a -= 3; //12
a *= 2; //24
a /= 4; //6
a %= 4; //2
4. 比较运算符
返回 bool 类型结果。
| 运算符 | 说明 |
|---|---|
| == | 等于 |
| != | 不等于 |
| > | 大于 |
| < | 小于 |
| >= | 大于等于 |
| <= | 小于等于 |
示例:
cs
int a = 10;
int b = 20;
Console.WriteLine(a == b); //False
Console.WriteLine(a != b); //True
Console.WriteLine(a > b); //False
Console.WriteLine(a < b); //True
5. 逻辑运算符
用于 bool 类型。
&&(逻辑且)
两边都为 True 才返回 True
cs
bool a = true;
bool b = false;
Console.WriteLine(a && b); //False
||(逻辑或)
有一个 True 即返回 True
Console.WriteLine(a || b); //True
!(逻辑非)
取反
cs
bool a = true;
Console.WriteLine(!a); //False
6. 短路运算符
&&
左边为 False
右边不执行
if(false && Test())
{
}
Test() 不会执行
||
左边为 True
右边不执行
if(true || Test())
{
}
Test() 不会执行
7. 位运算符
对二进制位操作。
| 运算符 | 名称 |
|---|---|
| & | 按位与 |
| | | 按位或 |
| ^ | 按位异或 |
| ~ | 按位取反 |
| << | 左移 |
| >> | 右移 |
按位与
cs
int a = 5; //0101
int b = 3; //0011
Console.WriteLine(a & b);
//0001 = 1
按位或
cs
Console.WriteLine(a | b);
//0111 = 7
按位异或
相同为0,不同为1
Console.WriteLine(a ^ b);
//0110 = 6
左移
int a = 5;
Console.WriteLine(a << 1);
//10
相当于乘以2
右移
cs
int a = 8;
Console.WriteLine(a >> 1);
//4
相当于除以2
8. 条件运算符(三元运算符)
语法:
条件 ? 表达式1 : 表达式2
示例:
cs
int age = 20;
string result = age >= 18
? "成年人"
: "未成年人";
Console.WriteLine(result);
9. 空合并运算符
??
判断是否为 null
string name = null;
string result = name ?? "默认名称";
Console.WriteLine(result);
输出:
默认名称
??=
为空才赋值
string name = null;
name ??= "Admin";
Console.WriteLine(name);
10. 空条件运算符
避免空引用异常。
Person p = null;
string name = p?.Name;
等价于:
cs
string name = p == null
? null
: p.Name;
11. 类型转换运算符
is
判断类型
cs
object obj = "Hello";
Console.WriteLine(obj is string);
输出:
True
as
安全转换
cs
object obj = "Hello";
string str = obj as string;
失败返回 null。
12. typeof 运算符
获取类型信息
cs
Type t = typeof(string);
Console.WriteLine(t.Name);
输出:
cs
String
13. sizeof 运算符
获取类型字节数
cs
Console.WriteLine(sizeof(int));
输出:
4
常见类型:
| 类型 | 字节 |
|---|---|
| byte | 1 |
| short | 2 |
| int | 4 |
| long | 8 |
| float | 4 |
| double | 8 |
| decimal | 16 |
14. nameof 运算符
获取变量名称字符串。
cs
string name = "Tom";
Console.WriteLine(nameof(name));
输出:
name
常用于异常提示:
throw new ArgumentNullException(nameof(name));
15. 运算符优先级(常见)
从高到低:
cs
()
++ --
! ~
* / %
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
??
?:
=
示例:
int result = 2 + 3 * 4;
先乘后加:
2 + 12 = 14