?? 运算符在左侧为 null 时返回右侧默认值,否则返回左侧值;2. ??= 运算符仅在左侧为 null 时才将右侧值赋给左侧;3. 两者通过延迟计算避免不必要的性能开销且编译为高效 il 代码;4. 适用于简化 null 检查、默认值赋值、链式 null 判断、属性初始化及避免重复计算;5. 替代方案包括 if 语句、三元运算符、getvalueordefault()、扩展方法和模式匹配,但在代码简洁性和可读性上不如 ?? 和 ??=。

C# 中的
??(空合并运算符) 和
??=(空合并赋值运算符) 主要用于简化空值判断和赋值操作,让代码更简洁易读。它们能有效避免 NullReferenceException,并提供默认值。
解决方案
??运算符(空合并运算符):
??运算符允许你提供一个默认值,当左侧的操作数为
null时,该默认值会被返回。否则,返回左侧操作数的值。
string name = null; string displayName = name ?? "Unknown"; // displayName 将被赋值为 "Unknown" Console.WriteLine(displayName); // 输出: Unknown name = "Alice"; displayName = name ?? "Unknown"; // displayName 将被赋值为 "Alice" Console.WriteLine(displayName); // 输出: Alice
这里,如果
name是
null,
displayName将会被赋值为 "Unknown"。如果
name有值,
displayName将会直接使用
name的值。
??=运算符(空合并赋值运算符):
??=运算符仅在左侧的操作数为
null时,才将右侧的操作数赋值给左侧的操作数。
string message = null; message ??= "Hello, World!"; // 仅当 message 为 null 时,才赋值 Console.WriteLine(message); // 输出: Hello, World! message ??= "Goodbye!"; // 这一行不会执行任何操作,因为 message 已经有值 Console.WriteLine(message); // 输出: Hello, World!
在这个例子中,
message最初是
null,所以
??=运算符将其赋值为 "Hello, World!"。第二次使用
??=时,
message已经不是
null,所以赋值操作被跳过。
C#
??和
??=运算符的性能考量?
??和
??=运算符通常具有良好的性能,因为它们是 C# 编译器直接支持的语法糖。编译器会将这些运算符转换为高效的 IL (Intermediate Language) 代码。 但是,一些因素可能会影响性能:
操作数的复杂性: 如果左侧的操作数涉及复杂的计算或属性访问,那么计算
null检查之前的操作数本身可能会带来性能开销。
默认值的计算: 如果
??或
??=右侧的默认值需要进行昂贵的计算(例如,创建新对象或调用耗时的方法),那么只有在左侧操作数为
null时才会执行这些计算,这可以避免不必要的性能开销。 这种延迟计算是
??和
??=的一个优点。
JIT 编译器的优化: JIT (Just-In-Time) 编译器能够对使用
??和
??=运算符的代码进行优化,例如内联简单的操作。
在大多数情况下,
??和
??=运算符的性能开销可以忽略不计,并且它们带来的代码简洁性和可读性提升通常远大于潜在的性能损失。 然而,在性能敏感的代码段中,最好进行基准测试来评估其影响。
何时应该使用 C# 的
??和
??=运算符?
简化 Null 检查: 当你需要检查一个变量是否为
null,并根据情况提供默认值时,
??运算符非常有用。 这可以避免使用冗长的
if语句或三元运算符。
string name = GetNameFromDatabase(); // 假设这个方法可能返回 null string displayName = name ?? "Guest"; // 如果 GetNameFromDatabase() 返回 null,则使用 "Guest"
默认值赋值: 当你希望仅在变量为
null时才进行赋值时,
??=运算符非常方便。
List<string> items = null;
items ??= new List<string>(); // 仅当 items 为 null 时,才初始化列表
items.Add("Item 1");
链式 Null 检查: 你可以将
??运算符链接在一起,以便在多个可能为
null的值中选择第一个非
null值。
string result = value1 ?? value2 ?? value3 ?? "Default Value"; // result 将被赋值为 value1, value2, value3 中第一个非 null 的值, // 如果所有值都是 null,则赋值为 "Default Value"
简化属性初始化: 在构造函数或属性设置器中,可以使用
??=运算符来确保属性具有默认值,而无需编写额外的
if语句。
private string _description;
public string Description
{
get => _description;
set => _description ??= "No description provided."; // 仅在 _description 为 null 时赋值
}
避免重复计算: 当默认值的计算成本较高时,
??和
??=运算符的延迟计算特性可以避免不必要的性能开销。
string data = GetDataFromCache(); // 假设这个方法可能返回 null string result = data ?? LoadDataFromDatabase(); // 仅当 data 为 null 时,才从数据库加载数据
??和
??=运算符提高了代码的可读性和简洁性,尤其是在处理可能为
null的值时。
C# 中
??和
??=运算符的替代方案?
在 C# 中,
??和
??=运算符主要用于处理空值情况,但在没有这些运算符的情况下,仍然有其他方法可以实现类似的功能。
使用 if
语句: 这是最基本的方法,通过显式地检查变量是否为
null,然后根据结果执行不同的操作。
string name = GetName();
string displayName;
if (name == null)
{
displayName = "Guest";
}
else
{
displayName = name;
}这种方法比较冗长,但易于理解,并且在所有 C# 版本中都可用。
使用三元运算符 ?:
: 三元运算符提供了一种更简洁的方式来进行条件赋值。
string name = GetName(); string displayName = name == null ? "Guest" : name;
三元运算符比
if语句更简洁,但当条件逻辑复杂时,可能会降低可读性。
使用 GetValueOrDefault()
方法: 对于可空类型(如
int?、
DateTime?),可以使用
GetValueOrDefault()方法来获取值,并在值为
null时返回默认值。
int? age = GetAge(); int actualAge = age.GetValueOrDefault(18); // 如果 age 为 null,则 actualAge 为 18
GetValueOrDefault()方法仅适用于可空类型。
使用扩展方法: 可以创建自定义的扩展方法来模拟
??和
??=运算符的行为。
public static class NullExtensions
{
public static T Coalesce<T>(this T value, T defaultValue) where T : class
{
return value ?? defaultValue;
}
public static void CoalesceAssign<T>(this ref T value, T defaultValue) where T : class
{
if (value == null)
{
value = defaultValue;
}
}
}
// 使用扩展方法
string name = GetName();
string displayName = name.Coalesce("Guest");
string message = null;
message.CoalesceAssign("Hello, World!");这种方法允许你自定义空值处理逻辑,但需要编写额外的代码。
使用模式匹配(C# 7.0 及更高版本): 可以使用模式匹配来简化空值检查和赋值。
string name = GetName();
string displayName = name switch
{
null => "Guest",
_ => name
};模式匹配提供了一种更灵活的方式来处理多种情况,包括空值。
虽然
??和
??=运算符通常是处理空值的最佳选择,但这些替代方案在特定情况下可能仍然有用,或者在不支持这些运算符的旧版本 C# 中是必需的。
