当前位置:网站首页>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
边栏推荐
- Langchao Yunxi distributed database tracing (II) -- source code analysis
- Course of causality, taught by Jonas Peters, University of Copenhagen
- 基础篇——整合第三方技术
- Reentrantlock fair lock source code Chapter 0
- [OBS] the official configuration is use_ GPU_ Priority effect is true
- Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
- What is load balancing? How does DNS achieve load balancing?
- [note] common combined filter circuit
- 大二级分类产品页权重低,不收录怎么办?
- 基于卷积神经网络的恶意软件检测方法
猜你喜欢
Lecture 1: the entry node of the link in the linked list
Service mesh introduction, istio overview
基于微信小程序开发的我最在行的小游戏
ReentrantLock 公平锁源码 第0篇
【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
基于卷积神经网络的恶意软件检测方法
2022-07-07: the original array is a monotonic array with numbers greater than 0 and less than or equal to K. there may be equal numbers in it, and the overall trend is increasing. However, the number
12.RNN应用于手写数字识别
Malware detection method based on convolutional neural network
FOFA-攻防挑战记录
随机推荐
Basic types of 100 questions for basic grammar of Niuke
利用GPU训练网络模型
Mathematical modeling -- knowledge map
The whole life cycle of commodity design can be included in the scope of industrial Internet
Experience of autumn recruitment in 22 years
Handwriting a simulated reentrantlock
6.Dropout应用
ABAP ALV LVC template
They gathered at the 2022 ecug con just for "China's technological power"
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
C # generics and performance comparison
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
Su embedded training - Day9
Binder core API
AI遮天传 ML-初识决策树
New library launched | cnopendata China Time-honored enterprise directory
[OBS] the official configuration is use_ GPU_ Priority effect is true
2022-07-07: the original array is a monotonic array with numbers greater than 0 and less than or equal to K. there may be equal numbers in it, and the overall trend is increasing. However, the number
大二级分类产品页权重低,不收录怎么办?