微软现在已经将.NetFramework开源了,这意味着可以自己去查看源代码了。附上DateTime.cs的源码链接,以及IsLeapYear方法的源代码。虽然仅仅两三行代码,但在实际开发中,你可能一时间想不起闰年的计算公式,或者拿捏不准。封装好的方法为你节省大量时间。
DateTime.cs源码中IsLeapYear方法
?
- // Checks whether a given year is a leap year. This method returns true if // year is a leap year, or false if not.
- // public static bool IsLeapYear(int year) {
- if (year < 1 || year > 9999) { throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Year"));
- } Contract.EndContractBlock();
- return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }
文章中介绍了几个算是比较常用的方法,希望对大家的学习有所帮助,平时多阅读相关文章,积累经验。
注:相关教程知识阅读请移步到c#教程频道。










