当前位置:网站首页>C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
2022-04-23 03:02:00 【dotNET跨平台】
例子
比如说:我们有一个UserInformation类
public class UserInformation
{
public string Name { get; set; }
public List<string> Address { get; set; }
}
有下面一段代码,我们获取张三的第一个地址,如果地址为空,则返回上海市,常规做法如下
static void Main(string[] args)
{
UserInformation user = new UserInformation();
user.Name = "张三";
user.Address = null;
string address = user.Address?.First();
if (address == null)
{
address = "上海市";
}
}
于是微软的工程师觉得我们既然可以使用?.优化属性这种类型判空,那我们能不能判断当计算结果为空时,给他进行赋值呢?于是微软工程师说干就干,所以空合并运算符就应运而生了,我们的代码就变成了
static void Main(string[] args)
{
UserInformation user = new UserInformation();
user.Name = "张三";
user.Address = null;
string address = user.Address?.First()??"上海市";
}
当然有的小伙伴就说了,这样好像不太满足一种情况,比如说当我们的Address为空时,怎么给User.address赋值呢?,还是只能写成这种模式,治标不治本。
UserInformation user = new UserInformation();
user.Name = "张三";
user.Address = null;
if (user.Address == null)
{
user.Address= new List<string> { "上海市" };
}
于是微软工程师觉得你小瞧我,说我治标不治本,那我不服,我必须要搞一个空合并赋值运算符,于是这个代码就变成了
static void Main(string[] args)
{
UserInformation user = new UserInformation();
user.Name = "张三";
user.Address = null;
user.Address??= new List<string> { "上海市" };
}
今天的介绍就到此结束了,
最后大家如果喜欢我的文章,还麻烦给个关注并点个赞, 希望net生态圈越来越好!
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124262931
边栏推荐
- 树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
- Reverse a linked list < difficulty coefficient >
- Encapsulate components such as pull-down menu based on ele
- JS learning notes
- Table space capacity query and expansion of Oracle Database
- Microservices (distributed architecture)
- 《信息系统项目管理师总结》第五章 项目质量管理
- [hcip] detailed explanation of six LSAS commonly used by OSPF
- Traversée de l'arbre L2 - 006
- Face longitude:
猜你喜欢
Windows MySQL 8 zip installation
Leangoo brain map - shared multi person collaborative mind mapping tool
Innovation and management based on Scrum
Blazor University (12)组件 — 组件生命周期
Introduction to ACM [inclusion exclusion theorem]
How to use C language to realize [guessing numbers game]
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
Android high-level interview must ask: overall business and project architecture design and reconstruction
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Domestic lightweight Kanban scrum agile project management tool
随机推荐
HLS / chisel practice CORDIC high performance computing complex square root
Basic SQL (VIII) data update operation practice
Close the computer port
Encapsulate components such as pull-down menu based on ele
对.NET未来的一点感悟
Er and eer models
Log4j knowledge point record
Redis data server / database / cache (2022)
Regular object type conversion tool - Common DOM class
Reverse a linked list < difficulty coefficient >
[if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
[hcip] detailed explanation of six LSAS commonly used by OSPF
Shell learning notes -- shell processing of output stream awk
Processes and threads
Classification of technology selection (2022)
Essential qualities of advanced programmers
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南
Winsock programming interface experiment: implementation of ipconfig
JS using the parameters of art template