So there is the coalescing operator ??
that allows handy handling of null objects (IE. MyDisplayString = MyString ?? "n/a";
)
but is there a nice fancy operator for handling a similar situation on properties of objects? For instance lets say that the property you are interested in is a property of a property like: MyDataObject.MySubModel.MyProperty
If MyProperty
is null you want coalesce to "n/a". You can use ??
here, but what if MyDataObject
is null or MyDataObject.MySubModel
?
This also comes up with XML when trying to get optional attributes and elements of an element. IE: MyString = MyElement.Attribute("MyOptionalAttribute").Value ?? "n/a";
fails if the attribute isn't there.
Is there a nice fancy way of handling this scenario?