Using the null-conditional operator on the left-hand side of an assignment

The null propagation operator returns a value. And since you must have a variable on the left hand side of an assignment, and not a value, you cannot use it in this way.

Sure, you could make things shorter by using the tenary operator, but that, on the other hand, doesn't really help the readability aspect.

Joachim Isaksson's comment on your question shows a different approach that should work.


As Joachim Isaksson suggested in the comments, I now have a method SetData(Data data) and use it like this:

MyPage1?.SetData(this.data);
MyPage2?.SetData(this.data);
MyPage3?.SetData(this.data);

I came up with the following extension,

public static class ObjectExtensions
{
    public static void SetValue<TValue>(this object @object, string propertyName, TValue value)
    {
        var property = @object.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
        if (property?.CanWrite == true)
            property.SetValue(@object, value, null);
    }
}

Which may be called globally; this only works on public properties.

myObject?.SetValue("MyProperty", new SomeObject());

The following improved version works on anything,

public static void SetValue<TObject>(this TObject @object, Action<TObject> assignment)
{
    assignment(@object);
}

And may also be called globally,

myObject?.SetValue(i => i.MyProperty = new SomeObject());

But the extension name is somewhat misleading as the Action does not exclusively require an assignment.