dd_
dd_

Reputation: 146

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?

Upvotes: 0

Views: 317

Answers (1)

GSerg
GSerg

Reputation: 78134

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

Reference:

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

Upvotes: 2

Related Questions