当前位置:网站首页>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 时,才能使用运算符 ??= 将其右操作数的值分配给左操作数。 这个还挺好使的
边栏推荐
- Introduction to ML regression analysis of AI zhetianchuan
- 深潜Kotlin协程(二十三 完结篇):SharedFlow 和 StateFlow
- "An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
- 5g NR system messages
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
- 手机上炒股安全么?
- 取消select的默认样式的向下箭头和设置select默认字样
- 【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
- Which securities company has a low, safe and reliable account opening commission
- Redis, do you understand the list
猜你喜欢

基于人脸识别实现课堂抬头率检测

Service Mesh介绍,Istio概述

玩轉Sonar

CVE-2022-28346:Django SQL注入漏洞

The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

赞!idea 如何单窗口打开多个项目?

国外众测之密码找回漏洞

Analysis of 8 classic C language pointer written test questions

fabulous! How does idea open multiple projects in a single window?
随机推荐
The weight of the product page of the second level classification is low. What if it is not included?
Implementation of adjacency table of SQLite database storage directory structure 2-construction of directory tree
Introduction to paddle - using lenet to realize image classification method II in MNIST
13. Enregistrement et chargement des modèles
英雄联盟胜负预测--简易肯德基上校
新库上线 | CnOpenData中华老字号企业名录
串口接收一包数据
8.优化器
Basic types of 100 questions for basic grammar of Niuke
Experience of autumn recruitment in 22 years
Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
基于人脸识别实现课堂抬头率检测
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
Malware detection method based on convolutional neural network
New library online | information data of Chinese journalists
深潜Kotlin协程(二十三 完结篇):SharedFlow 和 StateFlow
fabulous! How does idea open multiple projects in a single window?
14.绘制网络模型结构
How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
华泰证券官方网站开户安全吗?