Shorthand for nested null checking C#
Posted
by Myster
on Stack Overflow
See other posts from Stack Overflow
or by Myster
Published on 2010-05-14T02:07:46Z
Indexed on
2010/05/14
2:14 UTC
Read the original article
Hit count: 405
c#
|language-features
As far as I know there is not a significantly more elegant way to write the following....
string src;
if((ParentContent!= null)
&&(ParentContent.Image("thumbnail") != null)
&&(ParentContent.Image("thumbnail").Property("src") != null))
src = ParentContent.Image("thumbnail").Property("src").Value
Do you think there should be a C# language feature to make this shorter?
And if so, what should it look like?
for example, something like extending the ?? operator
string src = ParentContent??.Image("thumbnail")??.Property("width")??.Value;
Apologies for the rather contrived example, and my over-simplified solution.
© Stack Overflow or respective owner