In general, the solution uses modulo arithemtics:
DayOfWeek _RESETDAY = ...; int shift = 1; // can be positive or negative // + 7) % 7 to support negative shiftΒ΄s DayOfWeek result = (DayOfWeek) ((((int)_RESETDAY + shift) % 7 + 7) % 7);
Probably the best incarnation is to hide the combinatorial formula in the extension method:
public static class DayOfWeekExtensions { public static DayOfWeekShift(this DayOfWeek value, int shift) { return (DayOfWeek) ((((int)value + shift) % 7 + 7) % 7); } } ... var result = _RESETDAY.Shift(1);
and slightly reduced (only works in all cases if the negative shift is not lower than -7):
return (DayOfWeek)(((int)value + shift + 7) % 7);
source share