csharp:运算符

这是本文档旧的修订版!


在 C# 中,`?` 和 `??` 运算符主要用于处理 null 值(空值) 以及 条件逻辑。它们不仅能简化代码,还能显著提高代码的可读性和安全性(防止空引用异常)。

以下是这两个符号在不同场景下的详细含义和用法:

— ### 1. ?运算符 (单问号)

`?` 在 C# 中有三种主要的用法:

#### A. 可空类型修饰符 (Nullable Type Modifier) 用于定义一个值类型(如 `int`, `double`, `bool`, `DateTime` 等)可以存储 `null`。

* 原理:`int?` 实际上是 `System.Nullable<int>` 的简写。 * 场景:数据库中的数字字段可能为空,或者你需要表示“未设置”的状态。

```csharp int a = 10; 正常 int,不能赋值 null int? b = null; 可空 int,可以赋值 null ```

#### B. 三元条件运算符 (Ternary Conditional Operator) 这是 `if-else` 语句的简写形式。 * 语法:`condition ? true_value : false_value` * 含义:如果条件为真,返回冒号左边的值;否则返回冒号右边的值。

```csharp int x = 10; string result = (x > 5) ? “大于5” : “小于等于5”; ```

#### C. Null 条件运算符 (Null-conditional Operator) - C# 6.0+ 也称为“安全导航运算符”。用于在访问成员(属性、方法)或索引器之前检查对象是否为 `null`。 * 语法:`obj?.Property` 或 `arr?[index]` * 含义:如果对象不为 `null`,则访问成员;如果对象为 `null`,则整个表达式直接返回 `null`,而不会抛出 `NullReferenceException`。

```csharp string text = null; int? length = text?.Length; length 将会是 null,而不是抛出异常 ``` — ### 2. ?? 运算符 (双问号) `??` 主要用于提供默认值。 #### A. Null 合并运算符 (Null-coalescing Operator) 用于判断左侧的操作数是否为 `null`。 * 语法:`left ?? right` * 含义:如果 `left` 不为 `null`,则返回 `left`;如果 `left` `null`,则返回 `right`。 ```csharp string input = null; 如果 input 是 null,则 name 变成 “DefaultName” string name = input ?? “DefaultName”; ```

#### B. Null 合并赋值运算符 (Null-coalescing Assignment Operator) - C# 8.0+ 这是 `??` 和 `=` 的组合。

* 语法:`variable ??= value` * 含义:只有当左侧变量 `variable` 为 `null` 时,才将右侧的 `value` 赋值给它。如果左侧不为 null,则保持原值不变。

```csharp List<int> numbers = null; numbers ??= new List<int>(); 因为 numbers 是 null,所以会初始化新列表 numbers ??= new List<int>(); 此时 numbers 已不是 null,这行代码不会执行任何操作 ```

### 综合代码示例

为了更清晰地展示这些运算符的配合使用,请看下面的完整示例代码:

```csharp using System; using System.Collections.Generic;

public class Program {

  public static void Main()
  {
      // 1. 可空类型修饰符 (?)
      // int? 表示这个变量可以存储数字,也可以存储 null
      int? nullableInt = null;
      
      // 2. Null 合并运算符 (??)
      // 如果 nullableInt 是 null,则使用默认值 0
      int finalValue = nullableInt ?? 0; 
      Console.WriteLine($"1. Null合并结果: {finalValue}"); // 输出: 0
      // 3. 三元运算符 (?:)
      // 经典条件判断
      string status = (finalValue > 5) ? "High" : "Low";
      Console.WriteLine($"2. 三元运算结果: {status}"); // 输出: Low
      // 4. Null 条件运算符 (?.)
      Person person = null;
      // 如果 person 为 null,name 直接为 null,不会报错
      string name = person?.Name; 
      Console.WriteLine($"3. Null条件访问: '{name}'"); // 输出: '' (空)
      // 组合使用:安全访问 + 默认值
      // 如果 person 为 null 或者 Name 为 null,则显示 "Unknown"
      string displayName = person?.Name ?? "Unknown";
      Console.WriteLine($"4. 组合使用结果: {displayName}"); // 输出: Unknown
      // 5. Null 合并赋值运算符 (??=)
      List<string> tags = null;
      
      // 因为 tags 是 null,所以进行赋值
      tags ??= new List<string>(); 
      tags.Add("C#");
      
      // 因为 tags 已经不是 null 了,所以右边不执行
      tags ??= new List<string>(); 
      
      Console.WriteLine($"5. 列表元素数量: {tags.Count}"); // 输出: 1
  }

}

定义一个简单的类用于测试 public class Person { public string Name { get; set; } } ``` ### 总结表 | 运算符 | 名称 | 语法示例 | 解释 | | `?` | 可空类型 | `int? x` | 允许值类型为 null。 | | `?:` | 三元运算符 | `a ? b : c` | 如果 a 为真选 b,否则选 c。 | | `?.` | Null 条件 | `obj?.Prop` | 如果 obj 非空则访问 Prop,否则返回 null。 | | `??` | Null 合并 | `a ?? b` | 如果 a 非空返回 a,否则返回 b (默认值)。 | | `??=` | Null 合并赋值 | `a ??= b` | 仅当 a 为 null 时,将 b 赋值给 a。 |

该主题尚不存在

您访问的页面并不存在。如果允许,您可以使用创建该页面按钮来创建它。

  • csharp/运算符.1763542549.txt.gz
  • 最后更改: 2025/11/19 16:55
  • 张叶安