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

C# 中的
??
??=
解决方案
??
??
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
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
??=
??=
message
null
C#
??
??=
??
??=
操作数的复杂性: 如果左侧的操作数涉及复杂的计算或属性访问,那么计算
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#的??和??=运算符在空值处理中有何作用?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号