I have an enum that looks like this:
public enum MO
{
Learn = 0,
Practice = 1,
Quiz = 2,
SM2 = 3
}
public static partial class Extensions
{
public static bool IsLearn(this MO mode)
{
return mode switch
{
MO.Learn => true,
MO.Practice => false,
MO.Quiz => false,
MO.SM2 => false,
_ => throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString())
};
}
public static bool IsPractice(this MO mode)
{
return mode switch
{
MO.Learn => false,
MO.Practice => true,
MO.Quiz => false,
MO.SM2 => false,
_ => throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString())
};
}
public static bool IsQuiz(this MO mode)
{
return mode switch
{
MO.Learn => false,
MO.Practice => false,
MO.Quiz => true,
MO.SM2 => false,
_ => throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString())
};
}
}
It looks to me like there could be a way to simplify this code but I am not sure how to go about it. Does anyone have any suggestions on what I could do to as every time a new mode is added then now 12-13 lines are going to be added and it's just going to increase to more and more as the switch cases get bigger and bigger.
Would appreciate any advice on how I could fix this problem.