C# 中 ?、??、??=、?: 、?. 、?[] 各种问号的用法和说明

前言

在 C# 中,问号(?)远不止是一个简单的标点符号。随着语言版本的迭代更新,C# 围绕问号(?)发展出了一套强大而优雅的空值处理和条件表达机制。熟练掌握这些操作运算符不仅能大幅提升代码的简洁性和可读性,还能有效避免恼人的空引用异常,构建更加健壮的应用程序。

可空类型修饰符(?)

在 C# 中,值类型(如int、long、bool、DateTime等)默认不能为null。使用 ? 修饰符,我们可以将值类型转换为可空类型。

ini 复制代码
            int notNullableInt;// 非空int类型默认为 0
            int? nullableInt = null;
            bool? nullableBool = null;
            long? nullableLong = null;
            DateTime? nullableDate = null;

            // 检查是否有值
            if (nullableInt.HasValue)
            {
                Console.WriteLine($"整数值: {nullableInt.Value}");
            }
            else
            {
                Console.WriteLine("变量没有值(为null)");
            }

Null 合并运算符(??)

Null 合并运算符(??)如果左边的值不为null,则返回左边的值,否则返回右边的值。

ini 复制代码
        static void Main(string[] args)
        {
            string userName1 = "小明";
            string userName2 = null;
            var getUserName = userName1 ?? userName2 ?? "默认用户";

            Console.WriteLine(getUserName); // 输出: 小明

            string config1 = null;
            string config2 = null;
            string config3 = "DefaultConfig";
            string finalConfig = config1 ?? config2 ?? config3 ?? "FallbackConfig";
            Console.WriteLine(finalConfig); // 输出: DefaultConfig
        }

Null 合并赋值运算符(??=)

C# 8.0 引入的运算符,仅当左操作数为null时,才将右操作数的值赋给左操作数。这是懒加载模式的理想选择。

csharp 复制代码
        static void Main(string[] args)
        {
            string? name = null;
            name ??= "时光者";
            Console.WriteLine(name); // 时光者

            name ??= "大姚"; // 不改变
            Console.WriteLine(name); // 时光者


            //惰性初始化
            Dictionary<string, string>? cache = null;

            cache ??= new Dictionary<string, string>();
            cache["UserName"] = name;

            Console.WriteLine(cache["UserName"]);
        }

三元条件运算符(?:)

条件运算符(?:),又称三元运算符,是一种简洁的条件表达式形式。它对布尔表达式进行求值,并根据结果为true或false,选择性地返回两个表达式中的对应结果,为简单条件判断提供了一种比传统if-else语句更紧凑、表达力更强的语法形式。

ini 复制代码
        static void Main(string[] args)
        {
            int score = 80;
            string level = score >= 60 ? "Pass" : "Fail";
            Console.WriteLine(level);
        }

Null 条件成员访问运算符 (?.)

Null 条件成员访问运算符 (?.) 在访问对象成员(属性、方法、字段等)前先检查对象是否为 null。如果对象为 null,整个表达式返回 null 而不会抛出 NullReferenceException;如果对象不为 null,则正常访问成员。

csharp 复制代码
        static void Main(string[] args)
        {
            // 基本用法
            Person person = null;
            string name = person?.Name; // 不会抛出异常,name 为 null
            Console.WriteLine(name ?? "name is null"); // 输出: name is null
        }

Null 条件索引访问运算符 (?\[\])

Null 条件索引访问运算符 (?\[\]) 在使用索引器访问集合元素前先检查集合对象是否为 null。如果集合为 null,整个表达式返回 null 而不会抛出异常;如果集合不为 null,则正常访问索引位置的元素。

csharp 复制代码
        static void Main(string[] args)
        {
            List<string> names = null;
            string firstName = names?[0]; // 不会抛出异常,firstName 为 null
            Console.WriteLine(firstName ?? "No names available"); // 输出: No names available

            // 初始化列表后访问
            names = new List<string> { "时光者", "小袁", "大姚" };
            string secondName = names?[0]; // 安全访问索引为0的元素
            Console.WriteLine(secondName); // 输出: 时光者
        }

C#/.NET/.NET Core面试宝典

本文已收录至C#/.NET/.NET Core面试宝典中,更新C#/.NET/.NET Core面试干货前往开源地址查看:

相关推荐
影寂ldy34 分钟前
C# WinForms 窗体继承
开发语言·c#
苍狼唤13 小时前
WinForm练习知识补充(多线程)
c#
逝水无殇16 小时前
C# 字符串(String)详解
开发语言·后端·c#
小巧的砖头18 小时前
C#会重蹈覆辙吗?系列之2:反射及元数据的性能问题
开发语言·前端·c#
医疗信息化王工21 小时前
从零到一:基于 GGUF 格式部署 Unlimited-OCR 搭建企业级证件识别服务
图像处理·c#·ocr
zyh______1 天前
C#语法糖(按照实用性排序)
unity·c#
z落落1 天前
C#五大加密算法(AES、DES、MD5、RSA、SHA)
开发语言·c#
孝顺的书包1 天前
将《C# 调用非托管程序》一文中最后一种方法修改如下(篇幅原因简化了注释):
开发语言·c#
辰三1 天前
统信 UOS + GBase 8s 国产化环境部署实战:从虚拟机到数据库连接完整指南
linux·c#
标致的钢铁侠1 天前
c# 温故而知新: 线程篇(一)
java·jvm·c#