当前位置:网站首页>C# ?,?.,?? .....
C# ?,?.,?? .....
2022-07-07 23:11:00 【有诗亦有远方】
转载自这里
1.可空类型修饰符(?)
引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空。
例如:string str=null; 是正确的,int i=null; 编译器就会报错。
为了使值类型也可为空,就可以使用可空类型,即用可空类型修饰符"?“来表示,表现形式为"T?” 例如:int? 表示可空的整形,DateTime? 表示可为空的时间。
T? 其实是System.Nullable(泛型结构)的缩写形式, 也就意味着当你用到T?时编译器编译时会把T?编译成System.Nullable的形式。
例如:int?,编译后便是System.Nullable的形式。
2.三元(运算符)表达式(?: )
例如:x?y:z 表示如果表达式x为true,则返回y; 如果x为false,则返回z,是省略if{}else{}的简单形式。
3.空合并运算符(??)
用于定义可空类型和引用类型的默认值。
如果此运算符的左操作数不为null,则此运算符将返回左操作数,否则返回右操作数。
例如:a??b 当a为null时则返回b,a不为null时则返回a本身。 空合并运算符为右结合运算符,即操作时从右向左进行组合的。 如,“a??b??c”的形式按“a??(b??c)”计算。
4. NULL检查运算符(?.)
例如我们要获取一个Point序列的第一个点的X坐标,第一感觉会这么写: int firstX = points.First().X; 但是,老鸟会告诉你,这儿没有进行NULL检查,正确的版本是这样的:
int? firstX = null;
if (points != null)
{
var first = points.FirstOrDefault();
if (first != null)
firstX = first.X;
}
正确倒是正确了,代码取变得难读多了。在C# 6.0中,引入了一个 ?. 的运算符,前面的代码可以改成如下形式:
int? firstX = points?.FirstOrDefault()?.X; 从这个例子中我们也可以看出它的基本用法:如果对象为NULL,则不进行后面的获取成员的运算,直接返回NULL
需要注意的是,由于"?.“运算符返回的可以是NULL,当返回的成员类型是struct类型的时候,”?.“和”."运算符的返回值类型是不一样的。
Point p = new Point(3, 2);
Console.WriteLine(p.X.GetType() == typeof(int)); //true
Console.WriteLine(p?.X.GetType() == typeof(int?)); //true
5. "?[]"运算符:
int? first = customers?[0].Orders.Count();
如果数组customers为null,则表达式返回null;否则返回数组访问结果。
6.??=
仅当左操作数计算为 null 时,才能使用运算符 ??= 将其右操作数的值分配给左操作数。 这个还挺好使的
边栏推荐
- Cve-2022-28346: Django SQL injection vulnerability
- How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
- Reentrantlock fair lock source code Chapter 0
- 12.RNN应用于手写数字识别
- Handwriting a simulated reentrantlock
- Langchao Yunxi distributed database tracing (II) -- source code analysis
- 第一讲:链表中环的入口结点
- 3 years of experience, can't you get 20K for the interview and test post? Such a hole?
- 8道经典C语言指针笔试题解析
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
猜你喜欢

ReentrantLock 公平锁源码 第0篇

What does interface testing test?

Binder core API

DNS series (I): why does the updated DNS record not take effect?

玩轉Sonar

Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch

10.CNN应用于手写数字识别

8道经典C语言指针笔试题解析

letcode43:字符串相乘

Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
随机推荐
攻防演练中沙盘推演的4个阶段
大二级分类产品页权重低,不收录怎么办?
基于卷积神经网络的恶意软件检测方法
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
深潜Kotlin协程(二十三 完结篇):SharedFlow 和 StateFlow
New library online | information data of Chinese journalists
基于人脸识别实现课堂抬头率检测
什么是负载均衡?DNS如何实现负载均衡?
Experience of autumn recruitment in 22 years
【笔记】常见组合滤波电路
5.过拟合,dropout,正则化
赞!idea 如何单窗口打开多个项目?
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
What is load balancing? How does DNS achieve load balancing?
An error is reported during the process of setting up ADG. Rman-03009 ora-03113
Introduction to paddle - using lenet to realize image classification method II in MNIST
5G NR 系统消息
[reprint] solve the problem that CONDA installs pytorch too slowly
[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation
jemter分布式