0Pricing
C# Academy · 课时

空合并运算符

使用 ?? 和 ??= 提供备用值。

空合并运算符 是 CoddyKit 上的免费 C# Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 4 节课。

?? 运算符

空合并运算符 ?? 会在左操作数不为 null 时返回左操作数,否则返回右操作数。它是一种简洁的回退写法。

using System;

class Program {
    static void Main() {
        int? maybe = null;
        int value = maybe ?? -1;
        Console.WriteLine(value);
    }
}

?? 选择第一个非 Null 值

如果左侧包含值,就会使用该值,并忽略右侧。

using System;

class Program {
    static void Main() {
        int? a = 7;
        Console.WriteLine(a ?? 0);
    }
}

?? 与引用类型

?? 同样适用于 string 等引用类型,可以在值为 null 时提供默认文本。

using System;

class Program {
    static void Main() {
        string name = null;
        Console.WriteLine(name ?? "Guest");
    }
}

串联使用 ??

您可以串联多个回退值;系统会使用第一个非 null 值。

using System;

class Program {
    static void Main() {
        string a = null, c = "third";
        string b = null;
        Console.WriteLine(a ?? b ?? c ?? "last");
    }
}

?? 返回基础类型

当左侧为 int?、右侧为 int 时,结果是普通的 int,因为此时不可能再出现 null。

using System;

class Program {
    static void Main() {
        int? maybe = null;
        int sure = maybe ?? 0;
        Console.WriteLine(sure + 5);
    }
}

??= 运算符

空合并赋值 ??= 仅当左侧当前为 null 时,才会为其赋予右侧的值。

using System;

class Program {
    static void Main() {
        int? count = null;
        count ??= 10;
        Console.WriteLine(count);
    }
}

??= 保留现有值

如果变量已经有值,??= 就不会执行任何操作。

using System;

class Program {
    static void Main() {
        string config = "custom";
        config ??= "default";
        Console.WriteLine(config);
    }
}

使用 ??= 延迟初始化

??= 非常适合第一次设置值,并在之后保留该值。

using System;

class Program {
    static string cache = null;
    static string GetData() {
        cache ??= "loaded";
        return cache;
    }
    static void Main() {
        Console.WriteLine(GetData());
        Console.WriteLine(GetData());
    }
}

?? 与 GetValueOrDefault

两者都能提供回退值,但 ?? 更灵活:它适用于引用类型,并且回退值可以是任意表达式。

using System;

class Program {
    static void Main() {
        int? n = null;
        Console.WriteLine(n ?? 5);
        Console.WriteLine(n.GetValueOrDefault(5));
    }
}

在方法参数中使用 ??

可以直接使用 ??,在需要值的位置提供默认值。

using System;

class Program {
    static void Greet(string name) {
        Console.WriteLine("Hello, " + name);
    }
    static void Main() {
        string input = null;
        Greet(input ?? "friend");
    }
}

短路行为

?? 的右侧仅在左侧为 null 时求值,类似于短路求值。

using System;

class Program {
    static int Compute() {
        Console.WriteLine("Computed");
        return 42;
    }
    static void Main() {
        int? known = 1;
        int result = known ?? Compute();
        Console.WriteLine(result);
    }
}

快速检查

测试您对空合并运算符的理解。

回顾

?? 运算符在左侧不为 null 时返回左侧值,否则返回右侧值,并且可以进行链式使用。当提供非 null 的回退值时,结果类型会移除可为 null 性。

??= 仅在目标为 null 时赋值,因此非常适合设置默认值和进行延迟初始化。两者都只会在需要时计算右侧。

常见问题解答

「空合并运算符」课时是免费的吗?

是的 — 「空合并运算符」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 4 节课。

「空合并运算符」这节课中我会学到什么?

使用 ?? 和 ??= 提供备用值。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 C# Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「空合并运算符」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 C# Academy 课中编写并运行代码吗?

能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 可空值类型简介
  2. HasValue 与 Value 属性
  3. 空合并运算符
  4. 计算中的可空值
← 返回 C# Academy