循环中的歧义?

sur*_*asb 0 c# datetime

我得到的错误是"System.ArgumentOutOfRangeException未处理".它要求那个在1到12之间.但是看看调试器中的变量说它等于1,以及Debug.Writeline.

    int month, year, total;
    total = 0;
    DateTime dayToFind;

    for (year = 1001; year < 1201; year++){
        for (month = 1; month < 12; month++){
            dayToFind = new DateTime(year, month, DateTime.DaysInMonth(month, year));
            // The error points at the last occurance of month above.
            total = (dayToFind.DayOfWeek == DayOfWeek.Monday) ? 1 : 0;
        }

    }
Run Code Online (Sandbox Code Playgroud)

小智 8

你有DateTime.DaysInMonth()倒退的电话.把它改成这个:

DateTime.DaysInMonth(year, month)
Run Code Online (Sandbox Code Playgroud)

当你把year变量放在它的位置时month,它大于它可以的最大值(大于12),导致变量ArgumentOutOfRangeException.

DateTime.DaysInMonth()