有没有办法将此代码汇总为1-2行?
我的目标是返回,例如,我有一个DayOfWeek,它是星期一,我想在那之后的第二天(星期二)或那之后的n天。
switch (_RESETDAY)
{
case DayOfWeek.Monday:
_STARTDAY = DayOfWeek.Tuesday;
break;
case DayOfWeek.Tuesday:
_STARTDAY = DayOfWeek.Wednesday;
break;
case DayOfWeek.Wednesday:
_STARTDAY = DayOfWeek.Thursday;
break;
case DayOfWeek.Thursday:
_STARTDAY = DayOfWeek.Friday;
break;
case DayOfWeek.Friday:
_STARTDAY = DayOfWeek.Saturday;
break;
case DayOfWeek.Saturday:
_STARTDAY = DayOfWeek.Sunday;
break;
case DayOfWeek.Sunday:
_STARTDAY = DayOfWeek.Monday;
break;
default:
_STARTDAY = DayOfWeek.Tuesday;
break;
}
最佳答案
根据MSDN,这只是一个int枚举,范围从星期日(0)到星期六(6):
所以简单的数学应该做到这一点:
DayOfWeek nextDay = (DayOfWeek)(((int)_RESETDAY + 1) % 7);
如果需要,请用
+ 1
替换+ n
。关于c# - DayOfWeek获取下一个DayOfWeek(星期一,星期二…星期日),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32826723/