Search code examples
c#type-conversionintboolean

How can I convert from a bool to an int?


My code looks like this:

if (Settings.adp == true)
   App.DB.UpdateIntSetting(SET.Adp, 0);
else
   App.DB.UpdateIntSetting(SET.Adp, 1);

Is there a way I could get this down to one line by somehow converting the bool Settings.adp into an int (0 or 1)


Solution

  • You can use the ternary operator to consolidate it to one line:

    App.DB.UpdateIntSetting(SET.Adp, Settings.adp ? 0 : 1);
    

    From the documentation

    condition ? consequence : alternative