c# Single line object null check before assigning its property a value

Viewed 32

I am trying to simplify this logic into a single line of code perhaps using the null-coalescing operator, but I could not find an example of this exact scenario, though this is very common

if(obj!=null)
{
  obj.prop = false;
}

Where obj is an object

prop is a property

Can this be written in a single line without using the if statement for null check ? I know I can do var myVar = obj?.prop ; [This null check is only permitted for getting the value, not setting the value]

0 Answers
Related