enum DaysInMonth
{
Jan = 31, Feb = 28, Mar = 31, Apr = 30, May = 31, Jun = 30,
Jul = 31, Aug = 31, Sep = 30, Oct = 31, Nov = 30, Dec = 31
};
DaysInMonth dm = DaysInMonth.Dec;
Console.WriteLine($"Number of days in {dm} is {dm:D}");
获取7月份的天数为31天,预计12月份的天数为31
答案 0 :(得分:4)
对一个月内的天数进行硬编码并不是一个好主意。闰年怎么样?
一个月内获得天数的正确方法:
DateTime.DaysInMonth(year, month)
答案 1 :(得分:0)
您正在尝试使用Enum。所以请尝试以下代码。
enum DaysInMonth
{
Jan = 31, Feb = 28, Mar = 31, Apr = 30, May = 31, Jun = 30,
Jul = 31, Aug = 31, Sep = 30, Oct = 31, Nov = 30, Dec = 31
};
static void Main(string[] args)
{
DaysInMonth dm = DaysInMonth.Dec;
Console.WriteLine("Number of days in {0} is {1} : ", dm, (int)dm);
Console.ReadKey();
}