当前位置:网站首页>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
边栏推荐
- Get together to watch (detailed version) eat a few cents a day
- [ncnn] - the meaning of - 23300 in param
- Shell script learning notes -- shell operation on files sed
- Learn regular expression options, assertions
- Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
- Close the computer port
- Win view port occupation command line
- Summary of software test interview questions
- C# 11 的这个新特性,我愿称之最强!
- BLDC double closed loop (speed PI + current PI) Simulink simulation model
猜你喜欢

Binary tree

The interface request takes too long. Jstack observes the lock holding

Specific field information of MySQL export table (detailed operation of Navicat client)

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

Error installing Mongo service 'mongodb server' on win10 failed to start

Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl
![Niuke white moon race 6 [solution]](/img/c5/6c59378c3bb12efa60ab3a8cd2c943.png)
Niuke white moon race 6 [solution]

Blazor University (12)组件 — 组件生命周期

Dynamic sequence table + OJ

tf. keras. layers. Conv? D function
随机推荐
How to write the expected salary on your resume to double your salary during the interview?
Sonic cloud real machine tutorial
SQL statement - DDL
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
tf. keras. layers. MaxPooling? D function
樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
Airtrack cracking wireless network password (Dictionary running method)
OCR recognition PDF file
Essential qualities of advanced programmers
Basic SQL (VIII) data update operation practice
Openfeign service call
Introduction to ACM [inclusion exclusion theorem]
REINFORCE
Codeforces round 784 (Div. 4) (a - H)
Reverse a linked list < difficulty coefficient >
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
Dynamic sequence table + OJ
Winsock programming interface experiment: implementation of ipconfig
MySQL function syntax
Win view port occupation command line