当前位置:网站首页>C# ?,?.,?? .....
C# ?,?.,?? .....
2022-07-08 01:01:00 【Poetry and distance】
?
Reprinted here
1. Nullable type modifier (?)
Reference types can use empty references to represent a value that does not exist , The value type is usually not represented as empty .
for example :string str=null; That's right. ,int i=null; The compiler will report an error .
To make the value type also nullable , You can use nullable types , Use the nullable type modifier "?“ To express , In the form of "T?” for example :int? Represents a nullable shape ,DateTime? Indicates a time that can be empty .
T? It's actually System.Nullable( Generic structure ) Abbreviated form , That means when you use T? The compiler will compile T? Translate it into System.Nullable In the form of .
for example :int?, When compiled, it's System.Nullable In the form of .
2. Three yuan ( Operator ) expression (?: )
for example :x?y:z Means if the expression x by true, Then return to y; If x by false, Then return to z, It's the omission if{}else{} In a simple form .
3. Empty merge operator (??)
Default values for defining nullable and reference types .
If the left operand of this operator is not null, Then this operator will return the left operand , Otherwise, return the right operand .
for example :a??b When a by null When you return to b,a Not for null When you return to a In itself . The empty merge operator is the right merge operator , That is to say, it is combined from right to left during operation . Such as ,“a??b??c” In the form of “a??(b??c)” Calculation .
4. NULL Check operators (?.)
For example, we want to get a Point Of the first point of the sequence X coordinate , The first feeling would say : int firstX = points.First().X; however , The old bird will tell you , Not here NULL Check , The correct version is like this :
int? firstX = null;
if (points != null)
{
var first = points.FirstOrDefault();
if (first != null)
firstX = first.X;
}
That's right... That's right , Code extraction becomes much harder to read . stay C# 6.0 in , Introduced a ?. Operator , The previous code can be changed to the following form :
int? firstX = points?.FirstOrDefault()?.X; We can also see its basic usage from this example : If the object is NULL, Then, the subsequent operation of getting members is not performed , Go straight back to NULL
It should be noted that , because "?.“ The operator can return NULL, When the returned member type is struct When it comes to type ,”?.“ and ”." The return value type of the operator is different .
Point p = new Point(3, 2);
Console.WriteLine(p.X.GetType() == typeof(int)); //true
Console.WriteLine(p?.X.GetType() == typeof(int?)); //true
5. "?[]" Operator :
int? first = customers?[0].Orders.Count();
If the array customers by null, Then the expression returns null; Otherwise, return the array access result .
6.??=
Only if the left operand evaluates to null when , To use operators ??= Assign the value of its right operand to the left . This is quite easy to use
边栏推荐
- Image data preprocessing
- [reprint] solve the problem that CONDA installs pytorch too slowly
- Malware detection method based on convolutional neural network
- Get started quickly using the local testing tool postman
- C#中string用法
- 13.模型的保存和载入
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
- Codeforces Round #804 (Div. 2)(A~D)
- 130. 被圍繞的區域
- 130. Zones environnantes
猜你喜欢
2.非线性回归
接口测试要测试什么?
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
13.模型的保存和载入
4.交叉熵
Introduction to ML regression analysis of AI zhetianchuan
第一讲:链表中环的入口结点
Cve-2022-28346: Django SQL injection vulnerability
AI zhetianchuan ml novice decision tree
New library launched | cnopendata China Time-honored enterprise directory
随机推荐
【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
Malware detection method based on convolutional neural network
新库上线 | 中国记者信息数据
串口接收一包数据
Which securities company has a low, safe and reliable account opening commission
New library online | information data of Chinese journalists
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
14.绘制网络模型结构
My best game based on wechat applet development
C#中string用法
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
The weight of the product page of the second level classification is low. What if it is not included?
3.MNIST数据集分类
ReentrantLock 公平锁源码 第0篇
How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
新库上线 | CnOpenData中国星级酒店数据
英雄联盟胜负预测--简易肯德基上校
New library online | cnopendata China Star Hotel data
韦东山第二期课程内容概要
German prime minister says Ukraine will not receive "NATO style" security guarantee