当前位置:网站首页>可空类型修饰符与空合并运算符
可空类型修饰符与空合并运算符
2023-01-19 15:33:01【阿斯提尼】
一、可空类型修饰符
可以让一个值类型赋值为null,例如给一个struct赋值为null或判断一个struct是否被赋过值
if(m_TestStruct != null) {
}
"?"单问号:声明该值类型为可空类型
static void Main(string[] args)
{
int? a = null;
if (a == null)
{
Console.WriteLine("a为null");
}
Console.ReadKey();
}

声明一个可空类型有以下三种方式:
int? a = null;
int? b = new int?();
Nullable<int> c = new Nullable<int>();
二、空合并运算符
"??"双问号(空合并运算符):类似于条件表达式,可以判断当一个类型为null时返回另一个值
static void Main(string[] args)
{
int? a = null;
int? b = 1;
int c = 2;
int d = a ?? c;
int e = b ?? c;
Console.WriteLine(d);
Console.WriteLine(e);
Console.ReadKey();
}

注意:
- "??"的前面一个值必须是可空类型
- 空合并运算符为右结合运算符:a??b??c = a??(b??c)
- a??b??c??d??..??z的意义是返回第一个非空字符
边栏推荐
猜你喜欢
随机推荐
- 负电压是怎么产生的原理分析
- 迟滞比较器Hysteresiswindow和comparator(窗口比较器)原理
- 残留物与电子PCBA 的可靠性和三防漆涂敷前后可能导致电路板出现故障的变量
- 京东探索研究院 | 2023年十大科技趋势
- A variety of Chinese dialect voice landing applications, Microsoft's intelligent voice unlocks more interactive scenarios
- Is Alipay's annuity insurance worth buying?is it safe?
- Which is the top ten insurance for annuity insurance? Is it safe?
- Use MeterSphere beanshell global assertion reference JSONObject solution
- CNN+LSTM+Attention实现时间序列预测(PyTorch版)
- WebView加载heml代码简单应用
- CTA-Sensitive Behavior-AppOps Solution
- Beyond TensorFlow?Yann LeCun: "Why? PyTorch. That's why."
- 蜻蜓安全工作台程序编排简要说明
- WSL2安装systemd方法
- 安信证券开户安全吗?佣金是万几?
- 大智慧在上面开户安全吗?谁能告诉我一下
- 从合并石子学区间DP
- Golang的基本数据类型-基本使用
- 线扫相机DALSA--卡间同步
- 海康visionmaster-图像Bitmap和CmvdImage互转的方法
- 【threejs】根据点绘制直线
- 【threejs】threesjs 初学场景构建
- 第十三章 UML建模
- 为什么很多年轻人,都被 “伪自律” 给拖垮了?
- 小黑年前实习倒数第二天,又到了开心的疯狂星期四的leetcode之旅:剑指 Offer 27. 二叉树的镜像&&剑指 Offer 28. 对称的二叉树
- Servlet —— Servlet API
- 使用nvm管理node版本(window版)
- typescript学习笔记(三)
- vtkPolyData数据的空间变换
- 深入透析 类的 访问修饰符 public、 protected 、private
- C语言基础 — ( 函数——模块化设计)
- Wider Face+YOLOV8人脸检测
- Wider Face+YOLOV7人脸检测
- 【Android安全】Google Hardware-backed Keystore | SafetyNet | 远程证明Remote Attestation
- [转]SteamVR 1.x️一、实现手与物体交互——基于[CameraRig]
- PHP MySQL Where 子句
- PHP MySQL Order By 关键词
- 我理解的卓越工程
- 各种huggingface分词器对比
- PHP MySQL Order By keywords




