-1

Being a fan of null-coalescing operator and the null-propagating operator, I would like to assign something if the object is not null.

Something like:

myArray.FirstOrDefault()?.Value = newValue;

Instead of:

var temp = myArray.FirstOrDefault()

if(temp != null){
     temp.Value = newValue;
}

Is this possible?

(I am aware I could use SetValue() )

Athanviel
  • 199
  • 1
  • 3
  • 15

1 Answers1

2

With C# 8.0 you can do this

temp ??= newValue;
Alex - Tin Le
  • 1,982
  • 1
  • 6
  • 11