0
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}");

Getting Number of days in Jul is 31, expecting Number of days in Dec is 31

Saleem
  • 145
  • 1
  • 9

2 Answers2

4

Its not a good idea to hardcode the number of days in a month. What about leap years?

The correct way to gets days in a month:

DateTime.DaysInMonth(year, month)
Daniel
  • 2,744
  • 1
  • 31
  • 41
0

You are trying to Make use of Enum. So Try Following Code .

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();
        }
UJS
  • 853
  • 1
  • 10
  • 16