简体   繁体   中英

C# 6 Null Conditional Check

How do I convert this into C# 6 Null Conditional Check?

var durationhours = product.ProductAudioAsset.TotalLengthInSeconds != null
    ? (short?)TimeSpan.FromSeconds(product.ProductAudioAsset.TotalLengthInSeconds.Value).TotalHours
    : null;

where TotalLengthInSeconds is nullable short

You could create an extension method to help:

public static short SecondsAsTotalHours(this short value)
{
    return (short)TimeSpan.FromSeconds(value).TotalHours;
}

Then you can do this:

var durationhours = product.ProductAudioAsset.TotalLengthInSeconds?.SecondsAsTotalHours();

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