C# 6.0 Null Propagation Operator & Property Assignment

You're not the only one! SLaks raised this as an issue (now here)

Why can't I write code like this?

Process.GetProcessById(2)?.Exited += delegate { };

and after it was briefly closed as "By design"

the ?. Operator never produces an lvalue, so this is by design.

someone commented that it would be good for property setters as well as event handlers

Maybe add also properties setters into request like:

Object?.Prop = false;

and it was re-opened as a feature request for C#7.


You can't use the null-propagation operator in this way.

This operator allows to propagate nulls while evaluating an expression. It can't be used as the target of an assignment exactly as the error suggests.

You need to stick to the plain old null check:

if (a != null)
{
    a.Value = someValue;
}