当前位置:网站首页>C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
2022-04-23 03:06:00 【Dotnet cross platform】
Example
for instance : We have a UserInformation class
public class UserInformation
{
public string Name { get; set; }
public List<string> Address { get; set; }
}
There's the following code , Let's get Zhang San's first address , If the address is empty , Then return to Shanghai , The usual practice is as follows
static void Main(string[] args)
{
UserInformation user = new UserInformation();
user.Name = " Zhang San ";
user.Address = null;
string address = user.Address?.First();
if (address == null)
{
address = " Shanghai ";
}
}
So Microsoft engineers think that since we can use ?. This type of optimization attribute is null , Then can we judge when the calculation result is empty , Assign him a value ? So Microsoft engineers say do it , So the empty merge operator came into being , Our code becomes
static void Main(string[] args)
{
UserInformation user = new UserInformation();
user.Name = " Zhang San ";
user.Address = null;
string address = user.Address?.First()??" Shanghai ";
}
Of course, some friends said , This does not seem to satisfy a situation , For example, when our Address It's empty time , How to give User.address Assignment ?, It can only be written in this mode , Treat the symptoms, not the root cause .
UserInformation user = new UserInformation();
user.Name = " Zhang San ";
user.Address = null;
if (user.Address == null)
{
user.Address= new List<string> { " Shanghai " };
}
So Microsoft engineers think you underestimate me , Say I treat the symptoms but not the root cause , I won't accept it , I have to make an empty merge assignment operator , So this code becomes
static void Main(string[] args)
{
UserInformation user = new UserInformation();
user.Name = " Zhang San ";
user.Address = null;
user.Address??= new List<string> { " Shanghai " };
}
That's the end of today's presentation ,
Finally, if you like my article , Please pay attention and praise , hope net The ecosystem is getting better and better !
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334195.html
边栏推荐
- Niuke white moon race 5 [problem solving mathematics field]
- What kind of experience is it to prepare for a month to participate in ACM?
- Summary of software test interview questions
- How to write the expected salary on your resume to double your salary during the interview?
- ASP.NET 6 中间件系列 - 条件中间件
- Guangcheng cloud service can fill in a daily report regularly every day
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
- .NET7之MiniAPI(特别篇):.NET7 Preview3
- Creating wechat voucher process with PHP
- .Net Core 限流控制-AspNetCoreRateLimit
猜你喜欢
利用栈的回溯来解决“文件的最长绝对路径”问题
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
Summary of software test interview questions
Summary of interface automation interview questions for software testing
C#中切片语法糖的使用
[software testing] understand the basic knowledge of software testing
Passing object type parameters through openfeign
TP5 inherits base and uses the variables in base
Onenet connection process
Binary tree
随机推荐
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Recursion - outputs continuously increasing numbers
[format] simple output (2)
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
FileNotFoundError: [Errno 2] No such file or directory
What kind of experience is it to prepare for a month to participate in ACM?
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
tf. keras. layers. Conv? D function
Middle and rear binary tree
中后二叉建树
tf. keras. layers. Timedistributed function
[ncnn] - the meaning of - 23300 in param
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Dynamic sequence table + OJ
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
Tips in MATLAB
Openfeign service call
AspNetCore配置多环境log4net配置文件
TP5 inherits base and uses the variables in base