in Dot Net by
Q:
Null-coalescing assignment in C#

1 Answer

0 votes
by

C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null.

List<int> numbers = null; int? i = null; numbers ??= new List<int>(); numbers.Add(i ??= 17); numbers.Add(i ??= 20); Console.WriteLine(string.Join(" ", numbers)); // output: 17 17 Console.WriteLine(i); // output: 17

Related questions

0 votes
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya
0 votes
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya
0 votes
0 votes
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya
...