简体   繁体   中英

Null-coalescing operator ?? vs. ??= in C#

Is use of?? when checking a null using null-coalescing operator and there is??= in C# 8.0. There is the same functionality behavior and what is the advantage of using ==? operator.

int? a = null;   
int b = 10;    
a = a ??= 0;
a = a ?? b;

What is "bad practice" is highly opinionated, but in this case, not really.

The new operator ??= only replaces the common case of a = a?? b a = a?? b .

There are still many other users for ?? , eg if (a?.Foo()?? false) or Foo(a?? 0) or var c = a?? b var c = a?? b etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM