dimanche 10 octobre 2021

Is it possible to optimize these if-else statements?

I am new to C# and curious if there is some kind of better structure for this code:


if (RawMonth >= 01 && RawMonth <= 12)
{
    return RawMonth;
}
else if (RawMonth >= 51 && RawMonth <= 62)
{
    return RawMonth - 50;
}
else if (RawMonth >= 21 && RawMonth <= 32)
{
    return RawMonth - 20;
}
else if (RawMonth >= 71 && RawMonth <= 82)
{
    return RawMonth - 70;
}
else
{
    // just some of my custom exceptions
    throw new MonthCodeInvalidException();
}

Is it just me? Because those if-else statements look deprecated. Thank you.

Aucun commentaire:

Enregistrer un commentaire