C# 运算符
算术、比较、逻辑、位运算、空合并、以及 `??=`。
Key Points`??` 处理 null,`?.` 短路访问,`??=` 是「为 null 才赋值」。
算术与自增
C#
int a = 7, b = 3;
Console.WriteLine($"{a + b} {a - b} {a * b} {a / b} {a % b}");
Console.WriteLine(7.0 / 3.0);
int i = 1;
Console.WriteLine(i++); // 1,先返回再加
Console.WriteLine(++i); // 3,先加再返回Output
10 4 21 2 1
2.3333333333333335
1
3Integer division truncates: 7/3 == 2. Two ints never promote to double; write 7.0 / 3 or (double)a / b.
比较与逻辑
C#
Console.WriteLine($"{1 == 1} {1 != 2} {3 > 2} {3 <= 3}");
Console.WriteLine($"{true && false} {true || false} {!true}");
// 短路求值
string? s = null;
if (s != null && s.Length > 0)
{
Console.WriteLine(s);
}
else
{
Console.WriteLine("空字符串或 null");
}Output
True True True True
False True False
空字符串或 null空合并与空条件
C#
string? name = null;
Console.WriteLine(name ?? "默认名");
Console.WriteLine(name?.Length);
// ??=:为 null 才赋值
string? title = null;
title ??= "无标题";
Console.WriteLine(title);
title ??= "不会覆盖";
Console.WriteLine(title);
// 链式
string? a = null, b = null, c = "找到";
Console.WriteLine(a ?? b ?? c ?? "都没有");Output
默认名
无标题
无标题
找到| Operator | Meaning |
|---|---|
?? | 左边为 null 时取右边 |
??= | 左边为 null 时赋值 |
?. | 左边为 null 时整体为 null |
?[] | 索引版的空条件 |
! | 告诉编译器「这里肯定不是 null」 |
?. yields a nullable result: list?.Count is int? and cannot be assigned to int. The most common error after enabling nullable reference types.位运算
C#
int x = 0b1100, y = 0b1010;
Console.WriteLine(Convert.ToString(x & y, 2));
Console.WriteLine(Convert.ToString(x | y, 2));
Console.WriteLine(Convert.ToString(x ^ y, 2));
Console.WriteLine(Convert.ToString(~x & 0b1111, 2));
Console.WriteLine(x << 1);
Console.WriteLine(x >> 2);Output
1000
1110
110
11
24
3其他常用
C#
// 三元
int n = 5;
Console.WriteLine(n > 3 ? "大" : "小");
// is 类型检查
object o = "hello";
Console.WriteLine(o is string);
// is 带模式变量
if (o is string str)
{
Console.WriteLine(str.Length);
}
// as 转换,失败返回 null
var s2 = o as string;
Console.WriteLine(s2?.Length ?? -1);
// typeof 与 nameof
Console.WriteLine(typeof(int).Name);
Console.WriteLine(nameof(o));Output
大
True
5
5
Int32
o算术与自增
C#
int a = 7, b = 3;
Console.WriteLine($"{a + b} {a - b} {a * b} {a / b} {a % b}");
Console.WriteLine(7.0 / 3.0);
int i = 1;
Console.WriteLine(i++); // 1,先返回再加
Console.WriteLine(++i); // 3,先加再返回Output
10 4 21 2 1
2.3333333333333335
1
3整数除法截断:7/3 == 2。两个 int 相除不会自动变 double,要写成 7.0 / 3 或 (double)a / b。
比较与逻辑
C#
Console.WriteLine($"{1 == 1} {1 != 2} {3 > 2} {3 <= 3}");
Console.WriteLine($"{true && false} {true || false} {!true}");
// 短路求值
string? s = null;
if (s != null && s.Length > 0)
{
Console.WriteLine(s);
}
else
{
Console.WriteLine("空字符串或 null");
}Output
True True True True
False True False
空字符串或 null空合并与空条件
C#
string? name = null;
Console.WriteLine(name ?? "默认名");
Console.WriteLine(name?.Length);
// ??=:为 null 才赋值
string? title = null;
title ??= "无标题";
Console.WriteLine(title);
title ??= "不会覆盖";
Console.WriteLine(title);
// 链式
string? a = null, b = null, c = "找到";
Console.WriteLine(a ?? b ?? c ?? "都没有");Output
默认名
无标题
无标题
找到| 运算符 | 含义 |
|---|---|
?? | 左边为 null 时取右边 |
??= | 左边为 null 时赋值 |
?. | 左边为 null 时整体为 null |
?[] | 索引版的空条件 |
! | 告诉编译器「这里肯定不是 null」 |
?. 返回的是可空类型。list?.Count 的类型是 int?,不能直接赋给 int。这是开启可空引用类型后最常见的编译错误。位运算
C#
int x = 0b1100, y = 0b1010;
Console.WriteLine(Convert.ToString(x & y, 2));
Console.WriteLine(Convert.ToString(x | y, 2));
Console.WriteLine(Convert.ToString(x ^ y, 2));
Console.WriteLine(Convert.ToString(~x & 0b1111, 2));
Console.WriteLine(x << 1);
Console.WriteLine(x >> 2);Output
1000
1110
110
11
24
3其他常用
C#
// 三元
int n = 5;
Console.WriteLine(n > 3 ? "大" : "小");
// is 类型检查
object o = "hello";
Console.WriteLine(o is string);
// is 带模式变量
if (o is string str)
{
Console.WriteLine(str.Length);
}
// as 转换,失败返回 null
var s2 = o as string;
Console.WriteLine(s2?.Length ?? -1);
// typeof 与 nameof
Console.WriteLine(typeof(int).Name);
Console.WriteLine(nameof(o));Output
大
True
5
5
Int32
o