using System; // for DateTime
public static bool IsLeapYear(int year)
{
return DateTime.IsLeapYear(year);
}
This may be considered a "wicked cheat" for this exercise, by simply passing the year into the IsLeapYear method of the DateTime struct.
Although it is not in the spirit of this exercise, IsLeapYear
would be the idiomatic way to determine if a year is a leap year in the "real world".
Shortening
When the body of a function is a single expression, the function can be implemented as an expression-bodied member, like so
public static bool IsLeapYear(int year) =>
DateTime.IsLeapYear(year);
or
public static bool IsLeapYear(int year) => DateTime.IsLeapYear(year);
4th Sep 2024
·
Found it useful?