mardi 12 février 2019

Make multiple if statements into short statements

I am trying to make a comma-separated string based on boolean properties value in the model.

Code

        string options = string.Empty;

        if (model.Fruits.Value)
        {
            options += "FR,";
        }
        if (model.Drinks.Value)
        {
            options += "DR,";
        }
        if (model.Lunch.Value)
        {
            options += "LU,";
        }
        if (model.Dinner.Value)
        {
            options += "DI,";
        }

Can we make above code to be shorthand based, using ternary conditional operator (?:) or should I just leave the above one as it is, because it's more readable ?

Any suggestions would be great.

Aucun commentaire:

Enregistrer un commentaire