Implement Nested Ternary Operators in C#

chetan
 
on Feb 10, 2021 10:35 PM
1058 Views

I have the Below code:

                            if (reinsuranceYear >= 2021)
                            {
                                if (YieldTypeCode != "PA" && YieldTypeCode != "FA" && YieldTypeCode != "AP" && aph.YieldIndicatorCode != "MR"
                                  && YieldTypeCode != "BF" && YieldTypeCode != "VF")
                                {
                                    YieldTypeCode = "A ";
                                }
                            }
                            else
                            {
                                if (YieldTypeCode != "PA" && YieldTypeCode != "FA" && YieldTypeCode != "AP" && aph.YieldIndicatorCode != "MR")
                                {
                                    YieldTypeCode = "A ";
                                }
                            }

How to write the above code using a Conditional statement(A:B?C)?

Any help will be appreciated.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Feb 11, 2021 03:18 AM

Hi chetan,

Use nested Ternary Operators.

Refer below example.

int reinsuranceYear = 2020;
string YieldTypeCode = "PA";
string YieldIndicatorCode = "MR";
YieldTypeCode = reinsuranceYear >= 2021 ?
    YieldTypeCode != "PA" && YieldTypeCode != "FA" && YieldTypeCode != "AP" && YieldIndicatorCode != "MR" && YieldTypeCode != "BF" && YieldTypeCode != "VF" ? "A " : "B " :
    YieldTypeCode != "PA" && YieldTypeCode != "FA" && YieldTypeCode != "AP" && YieldIndicatorCode != "MR" ? "B " : "C ";