I have some simple old C # objects that are returned by the API, and have several layers of POCOs nested within them. I need to access the fields contained within these structures, but since the API leaves these nested objects empty when data is missing, I have to do a lot of null checks just to get into the field that I really want.
if(obj != null && obj.Inner != null && obj.Inner.Innerer != null) { ... }
The shortest form I came up with is using a ternary operator.
obj != null && obj.Inner != null && obj.Inner.Innerer != null ? obj.Inner.Innerer.Field : null;
Does C # have any way to do this without writing all these comparisons? I would really like something short and simple:
obj.Inner.Innerer.Field ?? null;
But this only checks the field for null.
null c #
Syon
source share