简体   繁体   中英

Ternary operator c# shorthand

I have class Segment which has four instances of class PricingDetailPer PricingDetailPerAdult , PricingDetailPerChild , PricingDetailPerSenior , PricingDetailPerInfant , and they all have property Availability .

In code i need to get property Availability but i need to check if Parent is exist. Is there shorthand like this:

return segment.PricingDetailPerAdult != null ? (this.Availability) : (continue checking);

where this refers to segment.PricingDetailPerAdult or some other property that i checking?

return segment.PricingDetailPerAdult?.Availability
       ?? segment.PricingDetailPerChild?.Availability
       ?? segment.PricingDetailPerSenior?.Availability
       ?? segment.PricingDetailPerInfant?.Availability;

Reference:

Null-conditional operators ?. and ?[]
?? operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM