当前位置:网站首页>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
边栏推荐
- Openfeign details show
- 荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
- Blazor University (12)组件 — 组件生命周期
- Ningde's position in the times is not guaranteed?
- 使用栈来解决”迷你语法分析器“的问题
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
- MAUI初体验:爽
- Guangcheng cloud service can fill in a daily report regularly every day
- 最通俗易懂的依赖注入与控制反转
- Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
猜你喜欢

TP5 customization in extend directory succeeded and failed. Return information

Detailed log display of openfeign call

tf. keras. layers. Conv? D function

Laravel new route file

Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel

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

最通俗易懂的依赖注入之生命周期

Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)

MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator

荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
随机推荐
c#可变参数params的介绍
先中二叉建树
最通俗易懂的依赖注入之生命周期
Close the computer port
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
ASP.NET和ASP.NETCore多环境配置对比
Laravel new route file
TP5 inherits base and uses the variables in base
Onenet connection process
What kind of experience is it to prepare for a month to participate in ACM?
Blazor University (12)组件 — 组件生命周期
Use split to solve the "most common words" problem
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
[format] simple output (2)
ASP.NET 6 中间件系列 - 条件中间件
FileNotFoundError: [Errno 2] No such file or directory
Systemctl start Prometheus + grafana environment
tf. keras. layers. Conv? D function
[ncnn] - the meaning of - 23300 in param